0

I'm doing some code, where i want to get the value of an input text introduced by the user

Get the value

       var inputTextValue = document.getElementsByClassName("something");        
       alert("inputTextValue.value);

Creation of the input text

 function createHour(){
    var hour = document.createElement("INPUT");
    hour.id = "something";
    defineButtonHour(hour);
    //after: -code to append to div
  }

function defineHour(hour) {
  hour.className = "something";
}

The alert just print undefined.

Mark
  • 77
  • 1
  • 3
  • 10

2 Answers2

4

getElementsByClassName returns an array with all the elements that match the class, you need to iterate over the array, or access the one that you want by providing an index"

alert(inputTextValue[0].value);

also theres an extra semicolon in your alert that shouldnt be there

StackOverMySoul
  • 1,957
  • 1
  • 13
  • 21
  • The extra semicolon was a mistake on the copy-paste. But your solution worked, you have my up vote and the accept of the answer – Mark Oct 14 '16 at 17:24
0
var inputTextValue = document.getElementsByClassName("something"); 

Returns a nodelist of all the nodes with the class something. A nodelist is kind of an array. you can write:

var inputTextValue = document.getElementsByClassName("something")[0]; 

If you can guarantee there will be at least one and you only want one.

Also, your alert has a quote where it doesn't need one:

alert(inputTextValue.value);
Ray Wadkins
  • 876
  • 1
  • 7
  • 16