简体   繁体   中英

merge textbox values with javascript

I have 5 textbox in html. I want to merge their values. All of them have one name. How can I do it?

<input type="text" name="textbox1">
<input type="text" name="textbox1">
<input type="text" name="textbox1">
<input type="text" name="textbox1">
<input type="text" name="textbox1">

If you want to select by the name attribute of your inputs, use getElementsByName like this:

var inputs = document.getElementsByName("textbox1");
var inputValues = "";
for(var index=0; index < inputs.length; index++) {
    inputValues += inputs[index].value;
}

Get the values as an array, and join it

var val = [].map.call( document.getElementsByName('textbox1'), (el) => el.value ).join('');

 var values = [].map.call( document.getElementsByName('textbox1'), (el) => el.value ).join(''); console.log(values)
 <input type="text" name="textbox1" value="a1-"> <input type="text" name="textbox1" value="a2-"> <input type="text" name="textbox1" value="a3-"> <input type="text" name="textbox1" value="a4-"> <input type="text" name="textbox1" value="a5-">

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