Search code examples
javascriptphpspecial-characters

Characters javascript


I have this

<?php
    echo "<script> $('#new-agency').click(function(){
         $('#AgencyTable').append('<tr id=/'3/'><td></td><td></td><td></td><td></td><td><input type=/'button/' class=/'updagetAgency/' value=/'Update/'/></td><td><input class=/'addAgency/' type=/'button/' value=/'Add/'/></td></tr>'); 
       });</script>";
?>

I am having problem with it, because of the mixing of ' and " in the same string,

I tried using '/' to escape but it doesn't seem to work, what can I do?


Solution

  • There's no reason here to use PHP! You needn't have an echo call at all.

    <script>
        $('#new-agency').click(function(){
             $('#AgencyTable').append("<tr id='3'><td></td><td></td><td></td><td></td><td><input type='button' class='updagetAgency' value='Update'/></td><td><input class='addAgency' type='button' value='Add'/></td></tr>"); 
        });
    </script>
    

    The other aspects of this problem:

    • inside a string delimited by ", there is no need to escape ' characters, and vice versa so we use " to delimit the string and ' for the quotes inside it
    • the escaping character is actually the backslash \, not the forward slash /.