12

I've been trying to figure out a way to replace part of a string in an input value, but haven't been able to get it working.

The input field looks like this:

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

I've tried:

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

and

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

but neither worked?

5 Answers 5

21
$('input[name=ids]').val(function(index, value) {
   return value.replace('54,', '');
});
Sign up to request clarification or add additional context in comments.

Comments

4

I've just tried this

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

and it worked so i assume that it's the way you call the element "$('input[name=ids]')", why don't you try using an id selector?

$('#ids')

7 Comments

the # in the jquery selector is for the 'id' attribute... won't select an element with a name of 'ids'...
@Damien-at-SF Maybe they've used <= IE8's getElementById() too much :D
forget it, it have nothing to do with the selector
@alex hahaha i forget there is a pre IE8 :P sometimes i forget what i did before jquery :P
i was just saying that he could try assigning an id attribute to his input and then use it as selector, not using the name as id...
|
1

try

replit = replit.replace("54,","");

Comments

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

hope that helps :)

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

:)

Comments

1

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

var matval = document.getElementById("MaterialNumber").value;

var matNum = matval.replace("P", "");

document.getElementById("MaterialNumber").value = "";

document.getElementById("MaterialNumber").value = matNum;

Generally I would not have cleared out the field before putting in the new string, but it did not work like that in this case, This might have been caused by the input method being a barcode scanner and set into a function based on a keypress of that said barcode scanner.

1 Comment

Generally I would not have cleared out the field before putting in the new string, but it did not work like that in this case, This might have been caused by the input method being a barcode scanner and set into a function based on a keypress of that said barcode scanner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.