fullOfQuestions
fullOfQuestions

Reputation: 453

Single Quote in Javascript Param

I have a function that, when clicked, fills in a field of the parent window. In this case, it's a name (text) field.

The problem I'm having is if the field has a single quote in it (ex. Bill's Chili) the function fails because it reads the single quote as the end of the parameter.

Here is the call:

href="javascript:selectItem('recipe','recipe_name','<recipe_description')"

Again, if the name is Bill's Chili, it causes a syntax error.

Is there a way to automatically convert that single quote to the HTML equivalent so it will read properly?

Thanks

Upvotes: 0

Views: 444

Answers (5)

fullOfQuestions
fullOfQuestions

Reputation: 453

The answer I found was completely different than I thought. The page itself is written is ASP (Sorry I forgot to mention that, I didn't think it mattered since the function was javascript and it was called in HTML).

Therefore, I just used this:

<%fixed_name = Replace(recipe_name,"'","") %>

And then used fixed_name instead of recipe_name in the function call.

Thanks for all your help, it set me in the right direction!

Upvotes: 1

Jerska
Jerska

Reputation: 12012

You could use str.replace

Just remplace " by &quot; et ' by &#39; . :)

But actually, I'm assuming you're getting all of that stuff from a php script (from some sort of storage), in which case you could escape the quotes directly with php, that would be way more safer.

Upvotes: 0

Branden S. Smith
Branden S. Smith

Reputation: 1161

For the single quotes in the field use \' More info on escape characters here.

href="javascript:selectItem('Bill\'s Chilli','recipe_name','<recipe_description')"

Upvotes: 2

timestopper
timestopper

Reputation: 440

You may try to use escaped 'double' quote like that:

href="javascript:selectItem(\"recipe\",\"recipe_name\",\"recipe_description\")"

Please let me know whether it works.

Upvotes: 0

Captain Jack Sparrow
Captain Jack Sparrow

Reputation: 449

try this

href='javascript:selectItem("recipe","recipe_name","<recipe_description")'

Upvotes: 0

Related Questions