Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem passing information to JS when string contains single quotes

Status
Not open for further replies.

casabella

Programmer
Aug 30, 2007
59
US
I have a page where a grid is defined. I give an option to remove a record by clicking an icon. The onClick looks like

Code:
onClick='RemoveVendor("65","LARRY C O'STEEN, CPA")'

As you can see, the name contains a single quote "O'STEEN". The function RemoveVendor fails when a record like this is chosen.

What should I use to avoid/fix this problem?

Thanks!


Jose
 
more a js question, Jose. but the answer is the same whichever language: you escape the illegal character.
Code:
onClick='RemoveVendor("65","LARRY C O\'STEEN, CPA")'

but actually, i prefer the other way around for xhtml best practice/compliance

Code:
onClick="RemoveVendor('65','LARRY C O\'STEEN, CPA')"
 
You know, I though of posting it in the JS forum but in the end, since this is dynamically generated using PHP, I posted it here.

I do understand the escaping the character with \ ... the problem is, how can I do that if the fields are the results of a query? (I guess I should had mentioned this on my original post).

Since I posted, I found addslashes() in php.net.


Regards,


Jose
 
Not to try and disrupt the process, but the first code is incorrect and the xhtml compliance does not feature in the second. In order to escape quotes in html (which incidentally are the single quotes in the first example), you need to use the html entity, not the usual backslash escape. Thus the first code in html would be
Code:
onClick=[blue]'RemoveVendor("65","LARRY C O\'[/blue][red]STEEN, CPA")'[/red]
where blue marks the onclick event and red marks erroneous code that will be probably ignored. To do it correctly, one needs to do:
Code:
onclick='RemoveVendor("65","LARRY C O[!]'[/!]STEEN, CPA")'

Finally, both single quotes and double quotes are xhtml compliant and none is preferred to another by the parser. It is true that most of the people use double quotes to surround the attributes, but finally it is only down to personal style. The only non-XHTML compliant part in the whole thing is the onClick, which should be all in lower-case, e.g. onclick.

___________________________________________________________
[small]Do something about world cancer today: PACT[/small]
 
absolutely right. Sunday evening aberration...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top