I am trying to save the html of a node to localstorage. Here is what I am trying to do. First look for the “strong” node and if the html of this node is “AM” only then will I get the time from the span node with class of “time”. It seems to work but not the way I would like it to. Rather it gets the time string from the very first span rather than the span that is above the “strong” node with html of “AM”. I hope this is clear if not I will try to explain as best I can.
HTML
<div>
<span class="time">Today 07:11 PM</span>
<strong>PM</strong>
</div>
<div>
<span class="time">Today 07:21 PM</span>
<strong>AM</strong>
</div>
Code
checkTime = setInterval(function(){
// get html of the 1st node
var firstNode = $("strong").html();
// if 1st node html is "AM" save time to local storage
if (firstNode == "AM") {
var userClock = $("span.time").html();
localStorage.setItem('clock', userClock);
}
}, 1000);
Live Proof