Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Object HTMLDivElement, get values?

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();

like image 200
basickarl Avatar asked Jan 14 '14 15:01

basickarl


1 Answers

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/

like image 98
Neil Kistner Avatar answered Oct 02 '22 20:10

Neil Kistner