<input type="text" id="fname"> <input type="text" id="lname"> <input type="button" value="Enter" id="fsubmit"> 

You need to get the value fname or lname by calling the getVal function. I tried

 function getValue(e){ console.log(e.value); } var fsubmit = document.getElementById("fsubmit"); fsubmit.onclick = getValue("fname"); 

The console issues undefined immediately upon boot, without clicking. It works only this way (at the input itself):

 function getValue(){ console.log("Button clicked, value "+ this.value); } var fname = document.getElementById("fname"); fname.onclick = getValue; 

    4 answers 4

     function getValue(e){ console.log(e.value); } var fsubmit = document.getElementById("fsubmit"); fsubmit.onclick = getValue("fname"); 

    getValue("fname"); calls the getValue function with the "fname" parameter - returning nothing. Thus, .onclick remains unassigned. Inside getValue it is expected that е is an input element with the value property, not a string. The value property of the string is undefined .

     function getValue(element){ console.log(element.value); } var fsubmit = document.getElementById("fsubmit"); fsubmit.onclick = function(event) { getValue(document.getElementById("fname")); }; 
    • Thanks for what I wanted. JavaScript is not a long study. - Vlad Ivanov
     var fsubmit = document.getElementById("fsubmit"); fsubmit.onclick = function() { console.log(document.getElementById("fname").value); }; 
       function getValue() { console.log(document.getElementById('fname').value); } 
         <input type="text" id="fname" onclick="$(this).val()"> <input type="text" id="lname" onclick="$(this).val()">