Created codepen URL for capturing only the BR elements with MAIN using
document.body.childNodes
codepen-http://codepen.io/nagasai/pen/MeybzK
First got all the childNodes of Body and then MAIN and filtered BR tags from that
Hope this is helpful for you
function allTags() { var c = document.body.childNodes; //console.log(c); var txt = ""; var i; for (i = 0; i < c.length; i++) { if (c[i].nodeName == "MAIN") { // alert(c[i].childNodes.length ) for (j = 0; j < c[i].childNodes.length; j++) { //alert(c[i].childNodes[j].nodeName); if (c[i].childNodes[j].nodeName == "BR") { txt = txt + c[i].childNodes[j]; } } } } console.log(txt); document.getElementById("demo").innerHTML = txt; }
HTML:
<p>11</p><br> <main> <br> <div>q1111</div><br> </main> <button onclick="allTags()">Tags</button> <div id="demo"></div>
maintag part and thenbrinside it..document.querySelector("main br")and possibly theDOMParser.