Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    trouble with quotes

    I have this line of javascript:

    Code:
    throne.innerHTML =  "<a href=home.php onclick='document.body.removeChild(document.getElementById("+"'throne'"+"));return false';>HEllo</a>";
    the problem is its coming out as

    Code:
    <a href="home.php" onclick="document.body.removeChild(document.getElementById('throne'));return false;">HEllo</a>
    i dont understand why javascript is not using the quotes as id like them to be used. ideally, i want this:

    Code:
    <a href="home.php" onclick="document.body.removeChild(document.getElementById(" throne'));return="" false';="">HEllo</a>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Use the escape character, '\',to specify which nested quote is to be referenced as it actually intended. It all has to do with nesting of the quotes as you have written it.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    What JMrkr said. BUT...

    What is WRONG with this:
    Code:
    <a href="home.php"
     onclick="document.body.removeChild(document.getElementById('throne'));return false;"
    >Hello</a>
    That's perfectly legal and correct JavaScript. Though I can't for the life of me see how you got it from the code you claimed you used. For starters, what put the "..." around home.php??

    Whereas this, that you say you want, is totally bogus:
    Code:
    <a href="home.php" 
    onclick="document.body.removeChild(document.getElementById(" throne'));return="" false';=""
    >HEllo</a>
    It doesn't even make sense. You can't use return= anything.

    Are you trying to put in the *VALUE* of the variable throne ??? Or, more likely, are you trying to use the ID of the DOM object referenced by your throne variable??

    In that case:
    Code:
    throne.innerHTML =  
        '<a href="home.php" onclick="document.body.removeChild(document.getElementById(\''
               + throne.id +'\'));return false';">HEllo</a>';
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    I *have* to ask: Why do you use href="home.php" when you are going to use return false to suppress the action of the <a> tag, anyway? Why not just href="#" ??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Quote Originally Posted by Old Pedant View Post
    I *have* to ask: Why do you use href="home.php" when you are going to use return false to suppress the action of the <a> tag, anyway? Why not just href="#" ??
    You don't even need to include the href= in the <a> tag in situations like this. Then once you've dropped that you can also drop the return false as then there is no default to disable.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •