I’m trying to strip out the single quote from my options in a select box, but the below doesn’t appear to be working:
$(function(){
$("#agencyList").each(function() {
$("option", $(this)).each(function(){
var cleanValue = $(this).text();
cleanValue.replace("'","");
$(this).text(cleanValue);
});
});
});
It still has the single quote. The select is built with a JSTL forEach loop. Can anyone see what might be going wrong?
You have to assign the new value by using
cleanValue = cleanValue.replace(...). Also, if you want to replace all single quotes, use a global RegEx:/'/g(which replaces all occurrences of single quotes):Another adjustment:
$(this)withthis, since it’s not necessary to wrap thethisobject in a jQuery object.Your code can be optimized even more my merging two selectors: