Akhil Akhil - 1 year ago 66
Javascript Question

Passing dynamic string variables with special character to JQuery Function

I generated custom checkbox for select or unselect my data. I have 2 custom functions for selecting and unselecting data. In the select method i need to pass 2 variables and it is field value.

My code:

<span custom-checkbox="true"
on-select="DataSelect('{this.id+'\',\'' +this.name}')"
on-unselect="DataUnselect('{this.id}')" id="{this.id}">

function DataSelect(id, value){
//my code

function DataUnselect(id, value){
//my code


name field contains special characters like quotes (''). the following are the sample data;

id = "1";
name= "hey'len";

Here i got the issue due to the quote('). So data manipulated as ;

DataSelect('1', 'hey'len')

So i got as "Uncaught SyntaxError: missing ) after argument list".

Please help me correct this

Answer Source

You can escape quotes from string using .replace()

id = "1";
name= "hey'len";
name.replace('\'', '\\\'');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download