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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With