0

I want to get the value from an input tag in HTML using JavaScript but I always get undefined. Help me solve this issue..

HTML code is as follows

<td><input type="text" class="number cost" value="10000"></td>

JavaScript is as follows

document.getElementsByClassName("cost").value;
Faizan Zahid
  • 39
  • 1
  • 3
  • 7

1 Answers1

3

getElementsByClassName() returns to you an array-like object, So if you have one item, it will return an one item in the array-like object. Get the first item by its index:

document.getElementsByClassName("cost")[0].value;
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112