181
rated 0 times
[
186]
[
5]
/ answers: 1 / hits: 48034
/ 8 Years ago, thu, april 7, 2016, 12:00:00
I need to add some CSS class based on some condition using JavaScript but facing issue in case my string contains a forward slash (/
). This is what my use case is
<div id=div_product-size-icon-121NM/L class=disabled></div>
<script>
var newProductCode = '121NM/L';
if( newProductCode.contains('/') ){
newProductCode = newProductCode.replace('/','///');
$('#'+'div_product-size-icon-'+newProductCode).addClass('active');
}
</script>
I have even tried newProductCode.replace('/','/');
but while running code, I am facing following error
JavaScript error: SyntaxError: unterminated string literal
I can not change HTML along with product code; the option for me is to change it in JS.
Here is a working js example: JS code
More From » jquery