How to replace part of input value in jquery?

I am trying to find a way to replace part of a string in an input value, but could not get it to work.

the input field is as follows:

<input type="text" value="20,54,26,99" name="ids" /> 

I tried:

 $('input[name=ids]').val().replace("54,",""); 

and

 var replit = $('input[name=ids]').val(); replit.replace("54,",""); $('input[name=ids]').val(replit); 

but didn’t work?

+6
jquery replace
source share
5 answers
 $('input[name=ids]').val(function(index, value) { return value.replace('54,', ''); }); 
+15
source share

I just tried this

 $('#title').val($('#title').val().replace("54,","")) 

and it worked so that I assume that this is how you call the element "$ (" input [name = ids] "), why don't you try using the id selector?

 $('#ids') 
+1
source share

to try

 replit = replit.replace("54,",""); 
0
source share
 var replit = $('input[name=ids]').val().replace("54,",""); $('input[name=ids]').val(replit); 

hope that helps :)

EDIT: if you need a working example: http://jsfiddle.net/Damien_at_SF/XQQC2/

:)

0
source share

I used this method, however I also included a line of code that replaces part of the line.

 var matval = document.getElementById("MaterialNumber").value; var matNum = matval.replace("P", ""); document.getElementById("MaterialNumber").value = ""; document.getElementById("MaterialNumber").value = matNum; 

In the general case, I would not clear the field before entering a new line, but in this case it did not work. Perhaps this is due to the fact that the input method is a barcode scanner and is set to a function based on pressing this barcode scanner.

0
source share

All Articles