Hello there I have a [Object HTMLDivElement] in which contains data that I would like to use.
I am using this. For example this.innerHTML.
My Div structure looks as the following:
<div class="hour"> <!-- <-- my Object referes to this div -->
<div class="time">2000</div>
<img class="weather-img" src="image/rain.png">
<div class="temp">-10</div>
</div>
I would like to extract the time and temp values, ergo 2000 and -10.
I've been reading the http://krook.org/jsdom/HTMLDivElement.html documentation but cannot seem to find a sleek solution to this, my best bet so far has been innerHTML but feels like there should be a more simple way. Anyone?
ANSWER: var time = $(this).find('.time').text();
Simple enough using jQuery:
var time = $('.time', this).text(),
temp = $('.temp', this).text();
A little longer way that was mentioned in the comments that works as well and shows a bit of chaining methods in jQuery:
var time = $(this).find('.time').text(),
temp = $(this).find('.temp').text();
A mod edited my code which might not be right, since if there is multiple elements with the class of time
and temp
on the page, you could get back the wrong data. My example before edit shows how you can scope the query better:
var time = $('.hour .time').text(),
temp = $('.hour .temp').text();
jQuery.text: http://api.jquery.com/text/
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