How to allow string to contain ' in javascript function

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












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');











share|improve this question







New contributor




minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Use quotes "" (:
    – Ele
    2 hours ago










  • @Ele Not working I tried that already
    – minion
    2 hours ago










  • json_encode? painless and flexible
    – cske
    2 hours ago














up vote
6
down vote

favorite












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');











share|improve this question







New contributor




minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Use quotes "" (:
    – Ele
    2 hours ago










  • @Ele Not working I tried that already
    – minion
    2 hours ago










  • json_encode? painless and flexible
    – cske
    2 hours ago












up vote
6
down vote

favorite









up vote
6
down vote

favorite











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');











share|improve this question







New contributor




minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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');








javascript php html






share|improve this question







New contributor




minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









minion

606




606




New contributor




minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






minion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Use quotes "" (:
    – Ele
    2 hours ago










  • @Ele Not working I tried that already
    – minion
    2 hours ago










  • json_encode? painless and flexible
    – cske
    2 hours ago
















  • Use quotes "" (:
    – Ele
    2 hours ago










  • @Ele Not working I tried that already
    – minion
    2 hours ago










  • json_encode? painless and flexible
    – cske
    2 hours ago















Use quotes "" (:
– Ele
2 hours ago




Use quotes "" (:
– Ele
2 hours ago












@Ele Not working I tried that already
– minion
2 hours ago




@Ele Not working I tried that already
– minion
2 hours ago












json_encode? painless and flexible
– cske
2 hours ago




json_encode? painless and flexible
– cske
2 hours ago












2 Answers
2






active

oldest

votes

















up vote
7
down vote



accepted










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)); ?>





share|improve this answer






















  • I gave an upvote but wondering how to contain all `, ' and " in a string?
    – Chaska
    2 hours ago











  • Thanks. Is there solution for backticks(just asking)
    – minion
    2 hours ago










  • @Chaska You can use str_replace to replace backticks with escaped versions
    – CertainPerformance
    2 hours ago

















up vote
0
down vote













I have faced this problem before, and solved it using json_encode() on the PHP response, like this:



cart.add(<?php echo json_encode($imeProizvoda);?>)





share|improve this answer




















  • What does json_encode do exactly in this example and is there more usages of it
    – minion
    2 hours ago










  • Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
    – D. Smania
    2 hours ago











Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






minion is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52732291%2fhow-to-allow-string-to-contain-in-javascript-function%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote



accepted










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)); ?>





share|improve this answer






















  • I gave an upvote but wondering how to contain all `, ' and " in a string?
    – Chaska
    2 hours ago











  • Thanks. Is there solution for backticks(just asking)
    – minion
    2 hours ago










  • @Chaska You can use str_replace to replace backticks with escaped versions
    – CertainPerformance
    2 hours ago














up vote
7
down vote



accepted










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)); ?>





share|improve this answer






















  • I gave an upvote but wondering how to contain all `, ' and " in a string?
    – Chaska
    2 hours ago











  • Thanks. Is there solution for backticks(just asking)
    – minion
    2 hours ago










  • @Chaska You can use str_replace to replace backticks with escaped versions
    – CertainPerformance
    2 hours ago












up vote
7
down vote



accepted







up vote
7
down vote



accepted






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)); ?>





share|improve this answer














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)); ?>






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 2 hours ago









CertainPerformance

55.1k142848




55.1k142848











  • I gave an upvote but wondering how to contain all `, ' and " in a string?
    – Chaska
    2 hours ago











  • Thanks. Is there solution for backticks(just asking)
    – minion
    2 hours ago










  • @Chaska You can use str_replace to replace backticks with escaped versions
    – CertainPerformance
    2 hours ago
















  • I gave an upvote but wondering how to contain all `, ' and " in a string?
    – Chaska
    2 hours ago











  • Thanks. Is there solution for backticks(just asking)
    – minion
    2 hours ago










  • @Chaska You can use str_replace to replace backticks with escaped versions
    – CertainPerformance
    2 hours ago















I gave an upvote but wondering how to contain all `, ' and " in a string?
– Chaska
2 hours ago





I gave an upvote but wondering how to contain all `, ' and " in a string?
– Chaska
2 hours ago













Thanks. Is there solution for backticks(just asking)
– minion
2 hours ago




Thanks. Is there solution for backticks(just asking)
– minion
2 hours ago












@Chaska You can use str_replace to replace backticks with escaped versions
– CertainPerformance
2 hours ago




@Chaska You can use str_replace to replace backticks with escaped versions
– CertainPerformance
2 hours ago












up vote
0
down vote













I have faced this problem before, and solved it using json_encode() on the PHP response, like this:



cart.add(<?php echo json_encode($imeProizvoda);?>)





share|improve this answer




















  • What does json_encode do exactly in this example and is there more usages of it
    – minion
    2 hours ago










  • Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
    – D. Smania
    2 hours ago















up vote
0
down vote













I have faced this problem before, and solved it using json_encode() on the PHP response, like this:



cart.add(<?php echo json_encode($imeProizvoda);?>)





share|improve this answer




















  • What does json_encode do exactly in this example and is there more usages of it
    – minion
    2 hours ago










  • Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
    – D. Smania
    2 hours ago













up vote
0
down vote










up vote
0
down vote









I have faced this problem before, and solved it using json_encode() on the PHP response, like this:



cart.add(<?php echo json_encode($imeProizvoda);?>)





share|improve this answer












I have faced this problem before, and solved it using json_encode() on the PHP response, like this:



cart.add(<?php echo json_encode($imeProizvoda);?>)






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









D. Smania

61814




61814











  • What does json_encode do exactly in this example and is there more usages of it
    – minion
    2 hours ago










  • Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
    – D. Smania
    2 hours ago

















  • What does json_encode do exactly in this example and is there more usages of it
    – minion
    2 hours ago










  • Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
    – D. Smania
    2 hours ago
















What does json_encode do exactly in this example and is there more usages of it
– minion
2 hours ago




What does json_encode do exactly in this example and is there more usages of it
– minion
2 hours ago












Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
– D. Smania
2 hours ago





Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
– D. Smania
2 hours ago











minion is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















minion is a new contributor. Be nice, and check out our Code of Conduct.












minion is a new contributor. Be nice, and check out our Code of Conduct.











minion is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52732291%2fhow-to-allow-string-to-contain-in-javascript-function%23new-answer', 'question_page');

);

Post as a guest













































































HkPlmr35N,eJRvg20hx,Ut Pq5NBS 0huOz URxoZGY
YU,g9ABM,7bSG FE12Ac1G1EHFL,4yU0t9,NO

Popular posts from this blog

How to check contact read email or not when send email to Individual?

How many registers does an x86_64 CPU actually have?

Displaying single band from multi-band raster using QGIS