Passing variable with single quote in javascript

后端 未结 6 1047
南方客
南方客 2021-01-27 06:25

I have an onClick call on a link:

 //this is working good

The problem is the variable insid

相关标签:
6条回答
  • 2021-01-27 06:34

    You can escape the quote with a backslash..

    fomateName('Andrew D\'souza');
    

    This should work anyway:

    var name = "Andrew D'souza";
    fomateName(name);
    
    0 讨论(0)
  • 2021-01-27 06:38

    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" -->
    
    0 讨论(0)
  • 2021-01-27 06:39

    Escape the quote with backslashes.

    <a onClick="fomateName('Andrew D\'souza')">
    //this is not working ,because present of single quote
    
    0 讨论(0)
  • 2021-01-27 06:41

    You can use escape character

    <a onclick="formateName('AdrewD\'souza')">
    
    0 讨论(0)
  • 2021-01-27 06:42

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

    <a onClick="fomateName('Andrew D\'souza')"> 
    
    0 讨论(0)
  • 2021-01-27 06:58

    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')">
    
    0 讨论(0)
提交回复
热议问题