Shaun Shaun - 8 days ago 4
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('-');
});


EDIT

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

Try this:

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

https://jsfiddle.net/7xeaLbkh/1/