1

Our form has a hidden input that contains all the required values we send to the server for server-side validation. This is formed of a comma-separated string:

i.e. <input type="hidden" value="name,address,telephone,email">

We now have new inputs (i.e. website) that appear on a radio button check and I can successfully add these to the value="" string:

var nowReq = $('input[name="required"]').val();
if ($('#website').is(':checked')) {
    $('input[name="required"]').val(nowReq + ',website');
}

But when a different radio is checked, I can't get it to remove ,website from the string.

I believe I need to grab the string, split it by comma, find and remove website and then re-join the string, but I'm not sure how to implement this (or if this is even the best way):

if ($('#fax').is(':checked')) {
    var splitReq = nowReq.split(',');
    // Something goes here?
    $('input[name="required"]').val(nowReq2);
}
1
  • Create a working fiddle Commented Apr 30, 2014 at 9:41

5 Answers 5

1

You can use string.indexOf(searchValue); and string.replace(oldValue, newValue); to search and remove non-selected elements or better pass Regular expression in the first parameter of string.replace(regex, newValue):

Non-regex Example:

var nowReq = $('input[name="required"]').val();
if ($('#website').is(':checked')) {
    nowReq = nowReq + ',website';
}
else {
    if(string.indexOf(',website') > -1) // If website is the last element
        nowReq = nowReq.replace(',website', '');
    else if(string.indexOf('website,') > -1) // If website is not the last element
        nowReq = nowReq.replace('website,', '');
}

$('input[name="required"]').val(nowReq);
Sign up to request clarification or add additional context in comments.

Comments

0

You simply need to remove the last element in your array and join it with the ',' character :

splitReq.pop();
$('input[name="required"]').val(splitReq.join(','));

2 Comments

who said its gonna be the last elemet
well, since he just added the value at the end of the string, it's always going to be the last element...
0

Try:

var splitReq = nowReq.split(',website').join("");

Comments

0

the following would work in most cases:

var s = "name,address,telephone,email";
s = s.replace(",address", "").replace("address,", "").replace("address", "");

Comments

0

I'd suggest that you avoid parsing the value string, and simply recalculate the value on the change of the relevant input elements; the following is a demonstrative example because of the lack of information presented in the question (a more specific example could be provided if we can see your HTML, and current jQuery, rather than a description of it):

$('input[type="checkbox"]').on('change', function(){
    var requiredString = $('input[type="checkbox"]').map(function(){
        if (this.checked) {
            return this.value;
        }
    }).get().join(',');
    $('input[name="required"]').val(requiredString);
}).change();

JS Fiddle demo.

References:

Comments

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.