Abela
Abela

Reputation: 1233

have a span text that removes commas from multiple input fields

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

Answers (4)

neli
neli

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

Sarbbottam
Sarbbottam

Reputation: 5580

Fiddle

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

Jayanth
Jayanth

Reputation: 329

To replace all occurrence, try

var res=str.replace(/,/g,'');

Upvotes: 0

Bla...
Bla...

Reputation: 7288

You can use .replace, like this:

var res = str.replace(",", ""); 

Upvotes: 0

Related Questions