Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow string to contain ' in javascript function

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');
        }
    }
like image 446
minion Avatar asked Oct 10 '18 03:10

minion


People also ask

Is there a Contains function in JavaScript?

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.

Can you use includes on a string JavaScript?

Definition and UsageThe includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.

Can Includes be used on a string?

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.

How do you check if a string contains a string in JavaScript?

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.


1 Answers

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)); ?>
like image 179
CertainPerformance Avatar answered Oct 22 '22 17:10

CertainPerformance