SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy single quotes error

    Hi,

    <script>
    function view(text)
    {
    document.getElementById("textbox").innerHTML="<input type='text' value='"+text+"'>";
    }
    </script>
    <a href="#" onclick="view('a \' \" ')">click to view textbox</a>
    <div id="textbox"></div>

    this is an example of what im trying to do in a site
    what this code does is when you click on hyperlink it shows a textbox in the div with text

    but single quote is causing problem and not letting it show the textbox even though i have escaped the single quote it will still not work double quote is working fine.

    my guess is since i first send it to function and then function prints it so the escape i did is removed and it starts giving error and will not print textbox in the div

    can anyone tell what should i add to escape and make it working
    i tested it in textarea its working there only it causes problem in textbox

    Thanks
    Farhan

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    HTML attributes should use double quotes. I'm not entirely sure what you want to appear in the text input box - a ' " ?:
    Code:
     <script>
    function view(text)
    {
    document.getElementById('textbox').innerHTML='<input type="text" value="'+text+'">';
    }
    </script>
    <a href="#" onclick="view('a \' &quot; ');">click to view textbox</a> 
    <div id="textbox"></div>

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is just a sample (a ' ") this is the place where text will come from mysql database and since it can have single quote so im having problem if there is single quote, double quote is working fine

    view ("text from database which has single \' quote")

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, the code I posted above should work, (note I changed the " to the correct character entity, &quot;).

  5. #5
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks,double quote is already working fine in the code where im using code like this the problem is in single quote here i have changed a little to show again

    <script>
    function view(text)
    {
    alert(text);
    document.getElementById('textbox').innerHTML="<input type='text' value='"+text+"'>";
    }
    </script>
    <a href="#" onclick="view('a \' ');">click to view textbox</a>
    <div id="textbox"></div>

    if you just paste this code in any html file and run it you will see that it will show alert with value a ' but it will not show the textbox while if you just remove the ' from function like change view ('a \' ') to view (' a ') it will show the textbox

    so the problem is the data is coming fine till alert but when it is trying to show in the textbox then it breaks up that is the problem i am having in a site im making.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You aren't using double quotation marks for your attributes. Try with the innerHTML code that I posted. Attributes should use double quotation marks. I think the problem is that the single quotation mark needs to be escaped again, but if you do what I said above, then I don't think you should have the problem.

  7. #7
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i ran the exact code you gave and it was not showing textbox at all until i removed the \' and &quot; from it then it showed the textbox


Bookmarks

Posting Permissions

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