Reputation: 1233
I have the following input fields and I would like to have a link above them that uses pure javascript (ie: no js libraries) that would remove the commas from all of the input fields.
Current:
<span id="remove">click to remove commas</span>
<input type="text" name="1" id="1" value="14,22,25,2,26,1,15,8,23"><br />
<input type="text" name="2" id="2" value="12,25,14,11,5,23,8,15,19"><br />
<input type="text" name="3" id="3" value="25,1,10,2,26,5,19,7,13,22"><br />
<input type="text" name="4" id="4" value="8,1,16,20,19,7,25,2,14,27"><br />
<input type="text" name="5" id="5" value="8,15,6,22,30,21,4,24,31,3">
Wanted Results:
<span id="remove">click to remove commas</span>
<input type="text" name="1" id="1" value="142225226115823"><br />
<input type="text" name="2" id="2" value="1225141152381519"><br />
<input type="text" name="3" id="3" value="2511022651971322"><br />
<input type="text" name="4" id="4" value="8116201972521427"><br />
<input type="text" name="5" id="5" value="8156223021424313">
Upvotes: 0
Views: 108
Reputation: 149
Set a common name for all the inputs, for example, values
. Then add calling of this method to span's onclick event:
function removeCommas() {
var values = document.getElementsByName("values");
for (var i = 0; i < values.length; i++) {
values[i].value = values[i].value.replace(/,/g, '');
}
}
See Fiddle.
Upvotes: 0
Reputation: 5580
JavaScript:
var removeSpan = document.querySelector('#remove');
removeSpan.addEventListener('click', function(e){
[].slice.call(document.querySelectorAll('[type="text"]')).forEach(function(text){
text.value = text.value.replace(/,/g, '')
});
});
Upvotes: 3