Home > front end >  how to get the name of an element in javascript created within it
how to get the name of an element in javascript created within it

Time:12-11

I am making a javscript library like reactJS. And, I am stuck with a problem.

//how to get the name of an element created in javascript in another function without parameter

//Example
let demo = document.createElement("div"):

function example(element){
  // this will get the name of the element ,ie.., div
  console.log(...)
  //should print "div" on the console
}
example(demo);

Can anybody help me?

Edit: This is just an example. The libray is more complex and long that I cant add it here.

CodePudding user response:

Try to get the element.localName, element.nodeName or element.tagName:

function example(element){
  console.log(element.localName); // should print 'div'
  console.log(element.nodeName); // should print 'DIV'
  console.log(element.tagName); // should print 'DIV'

}
  • Related