-1
<div class="view-content">
    <div class="views-row views-row-1">
    <div class="views-row views-row-2">
    <div class="views-row views-row-3">
    <div class="views-row views-row-4">
</div>

How can I in JavaScript/jQuery check whether the last <div> element (<div class="views-row views-row-4">) exist?

Some times, I just have 3 and others 4. When I just have 3, I would like to perform some CSS changes.

user3932702
  • 19
  • 1
  • 3

6 Answers6

7

You can look for the object and then check whether one was found:

if ($(".views-row-4").length) {
  // at least one element with class "views-row-4"
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
2

To check if a div exist ,you can you can use the following Jquery snippet.

if( $('.your-class').length )
{
    //CODE IF DIV EXIST
}
Varun
  • 1,946
  • 2
  • 11
  • 17
1

Try this like

$('.view-content').children().length
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
1

I think this might help you:

JS

var numClass = $(".views-row").length;
if(numClass == 3)
{
   /*Some Change in Your CSS*/
   $(".views-row").css("color","red");
}
divy3993
  • 5,732
  • 2
  • 28
  • 41
1

Check number of div inside the container div.

if ($('.view-content > div').length > 3) {
    // Do your code here
}
Prasanna
  • 779
  • 5
  • 13
0

Use .length in jquery

if($(".views-row").length == 3) {

}