html - How to identify tags in a node list in Javascript? -


when html looks this:

<div id="aa1"><h1>aaaa</h1><h2 class="center">bbbb</h2></div> 

and javascript looks this:

var list= document.getelementbyid("aa1").childnodes; 

i node list looks this:

list= nodelist[h1,h2.center] 

but, if html looks this:

<div id="aa1"><h3>cccc</h3><h1>aaaa</h1><h2 class="center">bbbb</h2></div> 

i node list looks this:

list= nodelist[h3,h1,h2.center] 

so, this:

if(list[0]==="<h3>"){console.log("yes, list[0] = <h3>");} 

but doesn't work.

how can identities of each tag in node list?

use .nodename property.

if(list[0].nodename==="h3"){console.log("yes, list[0] = <h3>");} 

there rare edge cases name returned lowercase, safety, add .touppercase() after .nodename.


also, though don't have text nodes contend with, if did, use .children instead of .childnodes.


Comments

Popular posts from this blog

css - Text drops down with smaller window -

php - Boolean search on database with 5 million rows, very slow -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -