Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery nested selector

Tags:

How can I grab the nested element "4"?

I tried:

var nights = $("div.nights h5 div.num").val(); 

and:

var nights = $("div.nights > h5 > div.num").val(); 

example:

<div class="nights">         <h5 class="biguns">             <div class="num">4</div>             Nights         </h5> </div> 
like image 337
holden Avatar asked Oct 08 '10 12:10

holden


1 Answers

Use .text() here instead, like this:

$("div.nights h5 div.num").text() // descendant selector //or this works too: $("div.nights > h5 > div.num").text() // child selector //or just $("div.num").text(); // chaining tag and class selector  

You can test it here, as you can see above, your selector is flexible, use what works on your overall markup. .val() is for input type elements, e.g. <input>, <select>, <textarea>, <button>...to get the text inside of any other element, use .text() instead.

like image 119
Nick Craver Avatar answered Nov 06 '22 05:11

Nick Craver