简体   繁体   中英

How to assign variable to value attribute in input element?

I need to assign variable to value attribute in the input element

Here is my input tag.

<input type="text" id="Name" class="form-control form-control-alternative" placeholder="Usernam" value="myValue">

Here is my variable.

var myValue = document.getElementById('userVal');

Anyone, please help me to fix this problem?

You can set input default value. You can not bind myValue in the html without some js framework. To get input value use change event. Check my code snippet.

 var input = document.getElementById('Name'); var myValue = 'Name example'; input.value = myValue; var myFunction = function (e) { console.log(e.target.value); } 
 <input type="text" id="Name" class="form-control form-control-alternative" placeholder="Usernam" onchange="myFunction(event)" value="myValue"> 

ASSIGN INPUT VALUE TO VARIABLE:

You need to assign the variable to the element's value, not the element itself. Also, your current input id is Name , not userVal . Change that to userVal then retrieve the value like this:

var myValue = document.getElementById('userVal').value;

Check the following Code Snippet for a practical example on how to retrieve an input value and assign it to a variable:

 /* JavaScript */ document.querySelector("button").addEventListener("click", function() { var myValue = document.getElementById('userVal').value; alert(myValue); }) 
 <!-- HTML --> <input type="text" id="userVal" class="form-control form-control-alternative" placeholder="Username"> <button>Check Value</button> 

ASSIGN VARIABLE TO INPUT VALUE:

To assign your input element's value to a variable, just reverse the above assignment like this:

var newValue = newValue;
document.getElementById('userVal').value = newValue;

Check the following Code Snippet for a practical example on how to assign a variable to your input element's value attribute:

 /* JavaScript */ document.querySelector("button").addEventListener("click", function() { var newValue = "newValue"; document.getElementById('userVal').value = newValue; }); 
 <!-- HTML --> <input type="text" id="userVal" class="form-control form-control-alternative" placeholder="Original" value="myValue"> <br /><br /> <button>Change Value</button> 

if you want to assign value to this input:

<input type="text" id="Name" class="form-control form-control-alternative" placeholder="Usernam" value="myValue">

you should use this code:

var myValue = document.getElementById('userVal').value;
document.getElementById("Name").value = myValue;

You can look to the documentation and example here: https://www.w3schools.com/jsref/prop_text_value.asp

In JavaScript, add value property to your code as:

    var myValue = document.getElementById("name").value

In HTML, use the same id to refer the input tag as:

    <input type="text" id="Name" class="form-control form-control-alternative" placeholder="Usernam" value="myValue">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM