+ 2
Get element inside element by class - JavaScript
How can i get first or last <p> element in Class by classname? <div class ="demo"> <p>some text</p> <p>some other text</p> </div>
7 Respostas
+ 3
Or if you use jQuery:
$("div .demo p:first")
+ 3
var demo = document.getElementsByClassName("demo")[0]; //first element with class demo
var pElms = demo.getElementsByTag("p");
pElms[0] // first element
pElms[(pElms.length - 1)] //last element
+ 2
I think 'children' method will help
+ 1
Thank you Rull Deef and Damyan Petkov
+ 1
var p = document.querySelectorAll(".demo p");
console.log("first p: " + p[0].innerText);
console.log("last p: " + p[p.length-1].innerText);
0
No jQuery