How to add and remove text to a string using jQuery -
i have web form hidden input field follows:
<input id="requiredfields" name="requiredfields" type="hidden" value="firstname,lastname,email,phone">
i want add , remove ",location
" based on condition using jquery follows:
$('input[name="locationstatus"]:radio').on("change", function() { if ($('input[name="locationstatus"]:radio:checked').val() == 'yes') { /* need syntax append text ',location' #requiredfields string */ } else { /* need syntax remove text ',location' #requiredfields string */ } });
thanks
using jquery's .val()
update value of requiredfields
$('input[name="locationstatus"]:radio').on("change", function() { if ($('input[name="locationstatus"]:radio:checked').val() == 'yes') { $('#requiredfields').val($('#requiredfields').val() + ',location'); } else { $('#requiredfields').val($('#requiredfields').val().replace(',location','')); } });
Comments
Post a Comment