Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Why is it doesn't work?
var x = document.getElementById('test').name; alert(x); // jhon
<div id='test' name='jhon'> its just a text </div>
Div elements are not allowed name attributes, so there is no matching property for them on the DOM.
name
If you want to store custom data on an element, use a data-* attribute.
data-*
If you really want to use invalid HTML you can access it with the getAttribute method.
getAttribute
Add a comment
You will have to use getAttribute method to get value of any attribute of html element
var x = document.getElementById('test').getAttribute("name"); console.log(x)
Try this:
var x = document.getElementById('test').getAttribute('name')
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.