How to get the value of an attribute in Javascript
Asked Answered
C

3

20

I have this input line which I am trying to extract the text of the value attribute:

    <input type="text" class="card-input small-font" 
ng-value="paymentVia.typeDisplay" readonly="" value="Credit card">

The function getAttribute("class") works fine and it returns card-input small-font but the function getAttribute("value") returns null. I tried .value as well but this returns an empty string.

Does anyone know why this is happening?

This is my JS:

function() {
   var x = document.getElementsByClassName("card-input small-font");
   var payment =x[18].value;

   return payment;
}
Culminant answered 17/6, 2016 at 11:13 Comment(7)
Show your html and your javascript.Submission
Use .value property than attribute..Dayledaylight
I added the html but it was removed from the question somehow :/ I'll try add it again. And I tried .value but it didn't work :/Culminant
@TamaraCaligari Function requires a name like: function foo(){} I don't see the name in your example.Nineteen
@Nineteen The function is to create a custom variable for Google Tag Manager and we don't use function names. See here for examples: apasters.com/blog/…Culminant
@TamaraCaligari Please don't forget to accept the answer that has helped you.Nineteen
var attribute = element.getAttribute(attributeName);Milky
N
42

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
Nineteen answered 17/6, 2016 at 11:15 Comment(4)
.value just returns an empty string for me :/Culminant
@TamaraCaligari Check maybe your value is empty.Nineteen
In the console I get the correct response though. Not sure why it does not behave the same way in the function.Culminant
@TamaraCaligari I don't know about your case, but it works here jsfiddle.net/rqy9ddkjNineteen
G
10

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

Gluteus answered 17/6, 2016 at 11:19 Comment(1)
As I mentioned in the other comment, element.value returns an empty string for me :/ Any ideas why?Culminant
F
-3

OR with jQuery you can get value from textbox like

var val1 = $(".class name").val();//to get value by class name
var val1 = $("#id").val();//to get value by id

Both will do same.

Regards

Forceps answered 17/6, 2016 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.