I have the following script which highlights some text, e.g 10%, but I want it to also highlight other text e.g 15%. At the moment, it only highlights the 10%. How can I make it highlight other words?
var text = '15% off';
var text = '10% off';
$( 'li' ).html( function ( i, html ) {
var regexp, replacement;
regexp = RegExp( '(' + text + ')', 'gi' );
replacement = '<span class="highlight">$1</span>';
return html.replace( regexp, replacement );
});
Not sure I understand what you are asking for. Do you want to do something for various percentages?
I donāt know JavaScript, but it seems to me that maybe you could use a CASE statement, or a series of IF-THEN-ELSEsā¦
In PHP you would do thisā¦
switch (n){
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
default:
code to be executed if n is different from all labels;
}
Part of the problem is (according to the sample code you provided) that you are declaring the variable ātextā twice (declare, set value, declare again, change value.) Once a variable is declared, declaring it any more times will result in an error. You should see an error message in your console.