How To Get The Value Of An Attribute In Javascript
Solution 1:
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.
<inputtype="text"id="profile" data-nationality="Eritrean" value="Simon">
and the javascript.
var el = document.getElementById("user-profile");
console.log(el.value) // Simonconsole.log(el.getAttribute("id")) // profileconsole.log(el.dataset.nationality) // EritreanSolution 2:
element.getAttribute("value") returns value which was set in the markup, which is not necessarily same as element.value.
Also, value attribute of an element is only synchronized one way - from markup to the object and vice versa doesn't happen.
So, if you want to get the value that is set programmatically, you need to write
element.value
else, if you need to get the value which was defined in the markup as
<input value="abc">
you need to do element.getAttribute("value")
Solution 3:
OR with jQuery you can get value from textbox like
var val1 = $(".class name").val();//to get value by class namevar val1 = $("#id").val();//to get value by idBoth will do same.
Regards
Post a Comment for "How To Get The Value Of An Attribute In Javascript"