jquery - Get tagname name value javascript - Stack Overflow

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

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

<preference name="webviewbounce" value="false" />

i need to get webviewbounce

This is what i know.

document.getElementsByTagName("preference")

But it doesnt give me the preference name. What i want is the tagname of name which is webviewbounce

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

<preference name="webviewbounce" value="false" />

i need to get webviewbounce

This is what i know.

document.getElementsByTagName("preference")

But it doesnt give me the preference name. What i want is the tagname of name which is webviewbounce

Share Improve this question asked May 17, 2017 at 4:24 MatarishvanMatarishvan 2,4323 gold badges42 silver badges71 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 2

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)
<preference name="webviewbounce" value="false" />

Try:

document.getElementsByName("webviewbounce");

This will get the element that has the name of webviewbounce

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' ) );

const p = document.getElementsByTagName('preference')

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

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

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().

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744537054a4579493.html

相关推荐

  • jquery - Get tagname name value javascript - Stack Overflow

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

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信