/    Sign up×
Community /Pin to ProfileBookmark

Passing strings with ‘ to Javascript functions

Hi, I would like to know how to pass strings that contain the ‘ sign in html when using javascript functions.

I have the editElement function in javascript that takes in 3 arguments in php. It so happens some products (in this case under the $product” variable) contain an ‘ in their name. I call the function when a button is clicked via the onclick event.

onClick=’editElement(1,2,”$product”)’

so what happens is when a product like “Jake’s Soup” is entered.

the command becomes:

onClick=’editElement(1,2,”Jake’s Soup”)’

which causes an error because of the ‘ in the “”.

How do you escape this character in javascript to make it work.

Thanks.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterMay 17.2006 — <i>
</i>onClick='editElement(1,2,"Jake[color=blue]&amp;[/color][color=blue]#39;[/color]s Soup")'

Another option:
<i>
</i>onClick=[color=blue]"[/color]editElement(1,2,[color=blue]&amp;quot;[/color]Jake's Soup[color=blue]&amp;quot;[/color])[color=blue]"[/color]

Or even both:
<i>
</i>onClick='editElement(1,2,[color=blue]&amp;quot;[/color]Jake[color=blue]&amp;[/color][color=blue]#39;[/color]s Soup[color=blue]&amp;quot;[/color])'
Copy linkTweet thisAlerts:
@felgallMay 17.2006 — or change $product to addslashes($product)
Copy linkTweet thisAlerts:
@oggaMay 17.2006 — You can use function escape():

escape("Jake's Soup")
×

Success!

Help @arzoo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...