tempori
tempori

Reputation: 45

How can count length div by id using javascript?

When click on click here it's still alert 0.

How can i count length div by id using javascript ?

function swipeDislike() {
  var $photo = $("div.content").find('#photo');
  alert($photo.length);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<div onclick="swipeDislike()">
  click here
</div>

<div id="content">
  <div id="photo">
    DIV content photo
  </div>
</div>

Upvotes: 1

Views: 1790

Answers (2)

Cory Kleiser
Cory Kleiser

Reputation: 2024

You have a typo instead of div.content use div#content

$("div#content").find('#photo') instead of $("div.content").find('#photo')

But the best way would be to just use $(“div#content #photo”); this way you won't need to use two expensive jQuery calls. Thanks Jaromanda X :)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<div onclick="swipeDislike()">
click here
</div>

<div id="content">
<div id="photo">
DIV content photo
</div>
</div>

<script>
	function swipeDislike() {
			var $photo = $("div#content #photo");
			alert($photo.length);
	}
</script>

Upvotes: 1

Mamun
Mamun

Reputation: 68943

Your selector is id not class.

Change var $photo = $("div.content").find('#photo');

To

var $photo = $("div#content").find('#photo');

Working Code Snippet:

function swipeDislike() {
  var $photo = $("div#content").find('#photo');
  alert($photo.length);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<div onclick="swipeDislike()">
  click here
</div>

<div id="content">
  <div id="photo">
    DIV content photo
  </div>
</div>

Upvotes: 1

Related Questions