Sunday, 15 July 2012

javascript - select and show all h2 elements inside their own div's -



javascript - select and show all h2 elements inside their own div's -

so have jquery:

var = document.getelementsbytagname("h2"); (var i=0, max=all.length; < max; i++) { $("h1").after($(all).html()); }

i'm trying select h2 elements on page, seems find first of them , post thrice.

here's html:

<h1>oversigt</h1> <div class="slide"> <h2>trin 1</h2> <p>tekst til afsnit 1.</p> </div> <div class="slide"> <h2>trin 2</h2> <p>tekst til afsnit 2.</p> </div> <div class="slide"> <h2>trin 3</h2> <p>tekst til afsnit 3.</p> </div>

and here link code on jsfiddle

what want post each headline of each div after h1 element, page displays: oversigt trin 1 trin 2 trin 3

you forgot utilize i. because of how trying this, elements going reversed. here suggestion

(var max = all.length, = max; > 0; i--) { $("h1").after($(all[i - 1]).html()); }

demo: http://jsfiddle.net/3flxu/

edit: looks cleaner, either works

(var max = all.length, = max - 1; >= 0; i--) { $("h1").after($(all[i]).html()); }

javascript jquery html html5

No comments:

Post a Comment