Skip to content Skip to sidebar Skip to footer

Get Tagname Name Value Javascript

I'm trying to get the tagname of name from the below line of code. I have to get the name from the below tagname using javascript

Solution 1:

Use document.querySelector to get the element. It will return the first matched element.Then use getAttribute to get the required attribute from the element. If there are multiple tag element with same tagname , use document.querySlectorAll

var getElem = document.querySelector('preference'),
  getNameProperty = getElem.getAttribute('name');
console.log(getNameProperty)
<preferencename="webviewbounce"value="false" />

Solution 2:

getElementsByTagName is going to return a collection of elements. You can then use getAttribute() to get the name property of the first item in the collection.

console.log( document.getElementsByTagName( "preference" )[0].getAttribute( 'name' ) );

Solution 3:

const p = document.getElementsByTagName('preference')

console.log(p[0])
// <preference name="webviewbounce" value="false">…</preference>console.log(p[0].getAttribute('name'))
// webviewbounce
<preferencename="webviewbounce"value="false" />

Solution 4:

Considering this as your first element of preference tag. this would give document.getElementsByTagName("preference")["0"].name the name. The "0" in the line code should be changed to the exact element.

In addition you can also use getAttribute('name') with getElementsByTagName().

Solution 5:

You can use getAttribute to get the name value of the tag.

You can try something like this.

var element = document.getElementByTagName("preference");
var name = element.getAttribute("name");
console.log(name);

Post a Comment for "Get Tagname Name Value Javascript"