Shaun Shaun - 11 months ago 43
jQuery Question

Strip commas from input value

I'm trying to add a class to the checkboxes in Contact Form 7 based from the value of the individual checkbox.

So far i've got it so it's adding the value as a class but i can't seem to get rid of the commas.

I've tried this but it's not working. Can anyone help please?

$( "#select-skip-form input[type=checkbox]" ).addClass(function(index) {
return $(this).val().replace(/,/g,'').toLowerCase().split(' ').join('-');


So for example i have a checkbox with the value "7 yard enclosed 'builders' skip" and i want to add this as a class on the field without the commas or spaces.

<input type="checkbox" name="checkbox-355" value="7 yard enclosed &#039;builders&#039; skip" />

So it ends up being like this:

<input type="checkbox" class="7-yard-enclosed-builders-skip" name="checkbox-355" value="7 yard enclosed 'builders' skip" />

Answer Source

Try this:

var $value = $('input').attr('value');
$value=$value.replace(/[^\/|\/a-zA-Z0-9 ]/g, "");
$value=$value.split(' ').join('-');

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download