lundi 23 février 2015

Get content of a div before and after id (or class) with native Javascript


I'm trying (in native Javascript, so without jQuery or anything) to get content before and after occurence of a div.


HTML:



<div id="thisIsWhatIWant">
<span>foo</span>
<div id="helloWorld">hello world</div>
<p>bar</p>
</div>


So I want to get <span>foo</span> and <p>bar</p> in separate vars.


JS:



var beforeElement_helloWorld = ...do something to get <span>foo</span>...
var afterElement_helloWorld = ...do something to get <p>bar</p>...


note:

There may be many more divs, spans etc in the HTML example.





Aucun commentaire:

Enregistrer un commentaire