Skip to content Skip to sidebar Skip to footer

Passing Variable With Single Quote In Javascript

Solution 1:

Try:

<a onClick="fomateName('Andrew D\'souza')"> <!-- this will work -->

\ use backslashes to escape '

Lets say if you have function like this =>

function fomateName(txt){
    alert(txt);
}

and invoking it from anchor =>

<a onClick="fomateName('Andrew D\'souza')"> <!-- this will alert "Andrew D'souza" -->

Solution 2:

Escape the quote with backslashes.

<a onClick="fomateName('Andrew D\'souza')">
//this is not working ,because present of single quote

Solution 3:

You can wrap it in double quotes like so:

   <a onClick="fomateName("Andrew D'souza")"> //this is not working ,because present of single quote

Never mind, just realized it already has double quotes, yeah use backslash for escape like so:

  <a onClick="fomateName('Andrew D\'souza')">

Solution 4:

Try this. Use backslash - It will escape the quote breaks

<a onClick="fomateName('Andrew D\'souza')"> 

Solution 5:

You can use escape character

<a onclick="formateName('AdrewD\'souza')">

Post a Comment for "Passing Variable With Single Quote In Javascript"