dotnetrocks
dotnetrocks

Reputation: 15

Jquery replace not working for inner strings

I need to replace ||| from all the input fields using jquery. But it is only replacing the first item. I need to use regex but I don't know how to add that to the string.

$(document).ready(function() {
    $("#layerPaper").children("div").each(function() {
        $val = $(this).children('input:text');
        $val.val($val.val().replace('|||', '\"'));
    });
}); 

Thanks.

Upvotes: 1

Views: 245

Answers (3)

Neil
Neil

Reputation: 3231

If you mean there are multiple instances of '|||' in an element then the replace you want is:

replace(/\|\|\|/g, '\"')); 

Upvotes: 4

user113716
user113716

Reputation: 322562

$("#layerPaper > div > input:text").val(function(i,val){ 
    return val.replace('|||', '\"');
});

Upvotes: 3

Brad Christie
Brad Christie

Reputation: 101614

You don't need regex, I think your problem lies in the following:

$val.val($val.val().replace('|||', '\"'));

The .val() function only works on the first element found. You can switch this by using $.each though, fairly simply:

$(this).children('input:text').each(function(i,e){
  $(e).val($(e).val().replace('|||','"'));
});

Upvotes: 0

Related Questions