How to get the value of an attribute in Javascript

Node values and Element Attributes are different parts of an html tag.
So, you have to use element.value instead.

This is a an example, to show you how you can fetch value, data, attribute from an input field.

The HTML input field.

<input type="text" id="profile" data-nationality="Eritrean" value="Simon">

and the javascript.

var el = document.getElementById("profile"); 

console.log(el.value) // Simon
console.log(el.getAttribute("id")) // profile
console.log(el.dataset.nationality) // Eritrean

Leave a Comment