How do I get all h1,h2,h3 etc elements in javascript?
With modern browsers you can do document.querySelectorAll(“h1, h2, h3, h4, h5, h6”) Or you could get cross-browser compatibility by using jQuery: $(“h1, h2, h3, h4, h5, h6”)
With modern browsers you can do document.querySelectorAll(“h1, h2, h3, h4, h5, h6”) Or you could get cross-browser compatibility by using jQuery: $(“h1, h2, h3, h4, h5, h6”)
You can get the first child of the body element with the firstChild property. Then use it as the reference. const p = document.createElement(“p”); p.textContent = “test1”; document.body.insertBefore(p, document.body.firstChild); I modernized your code for reasons 🙂
2016 update It’s been over 4 years since this question was posted and things progressed quite a bit. You can’t use: var els = document.getElementsByTagName(“a[href=”http://domain.example”]”); but what you can use is: var els = document.querySelectorAll(“a[href=”http://domain.example”]”); (Note: see below for browser support) which would make the code from your question work exactly as you expect: for … Read more
No, you can’t select multiple tags with a single call to getElementsByTagName. You can either do two queries using getElementsByTagName or use querySelectorAll. JSFiddle var elems = document.querySelectorAll(‘p,li’)
You need to convert the nodelist to array with this: <html> <head> </head> <body> <input type=”text” value=”” /> <input type=”text” value=”” /> <script> function ShowResults(value, index, ar) { alert(index); } var input = document.getElementsByTagName(“input”); var inputList = Array.prototype.slice.call(input); alert(inputList.length); inputList.forEach(ShowResults); </script> </body> </html> or use for loop. for(let i = 0;i < input.length; i++) { … Read more