-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.

4
  • if($(".views-row").length > 3) { Commented Jun 26, 2015 at 13:24
  • 1
    saying on what element you want to perform the CSS changes , might help provide a better solution. Commented Jun 26, 2015 at 13:26
  • yea see that not closing? does not matter Commented Jun 26, 2015 at 13:30
  • 2
    @user3932702 It does matter.There is every chance that you may not get desired output because of mistakes like that. You want proper answers, ask proper questions . Commented Jun 26, 2015 at 13:34

6 Answers 6

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" } 
Sign up to request clarification or add additional context in comments.

Comments

2

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

if( $('.your-class').length ) { //CODE IF DIV EXIST } 

Comments

1

Try this like

$('.view-content').children().length 

Comments

1

Check number of div inside the container div.

if ($('.view-content > div').length > 3) { // Do your code here } 

Comments

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"); } 

Comments

0

Use .length in jquery

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.