I have function that is going to display message containing product name. But problem appeared when Item contained ' inside. Can this be prevented to take whole string as it is and ignore ' character
Calling function
cart.add('<?php echo($imeProizvoda); ?>')
NOT WORKING FOR -> Razer Blade 15'
javascript
var cart = {
'add': function(product_id) {
addProductNotice('Proizvod dodat u korpu', '<h3>'+product_id+' dodat u <a href="cart.php">korpu</a>!</h3>', 'success');
}
}
js contains() Method. The contains() method is used to determines whether the collection contains a given item or not. If it contains the item then it returns true otherwise false. The JavaScript array is first transformed into a collection and then the function is applied to the collection.
Definition and UsageThe includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.
In JavaScript, includes() is a string method that determines whether a substring is found in a string. Because the includes() method is a method of the String object, it must be invoked through a particular instance of the String class.
To check if a substring is contained in a JavaScript string:Call the indexOf method on the string, passing it the substring as a parameter - string. indexOf(substring) Conditionally check if the returned value is not equal to -1. If the returned value is not equal to -1 , the string contains the substring.
You could use a template literal instead of '
delimiters, allowing you to use both '
and "
(relatively common characters), but you would have to escape backticks (which are relatively uncommon):
cart.add(`<?php echo($imeProizvoda); ?>`)
If you want to be able to use backticks as well, you can replace all backticks in your PHP variable with a backslash plus that backtick:
<?php echo(str_replace('`', '\\`', $imeProizvoda)); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With