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
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with value

    Hi,

    I have html variable as below:

    html +=
    "<div class='quickSearchResultDivUnselected' style='width:" + divWidh + "px;max-width:" + divWidh + "px'><a href='#' OnClick='javascript:test('" + title + "')>" + title + "</a></div>";

    The problem is with OnClick='javascript:test('" + title + "')>".

    How to make this link as <a OnClick='javascript:test("VALUE")'></a>, because now as result it looks like this and it doesn't work:
    <a OnClick='javascript:test('value')'></a>;
    So the problem is with ' and ". How to make it correctly in this long line of html variable ? Please help.

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    905
    Thanks
    0
    Thanked 118 Times in 117 Posts
    Code:
    <a OnClick='javascript:test(\"value\")'></a>;
    Single quotes are use to enclose the onclick, so the next level must be double quotes. As they are the same as those used to close the entire string the need to be escaped. The \ used to escape the double quotes will not be written to the page , but is solely used to make javascript understand that this quote does not mark the end of the string, but is a character within the string.

    If you had needed an extra set of quotes within these qotes you would have to write those as \'

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, now I used sytnax as below:

    html +=
    "<div class='quickSearchResultDivUnselected' style='width:" + divWidh + "px;max-width:" + divWidh + "px'><a style='text-decoration:none;color:#000000' href='#' OnClick='javascript:test(\" + title + \")'>" + title + "</a></div>";

    but I got as result string "+ title +" but title is a variable (not string) f.ex title = "test1" so how to use var in this syntax and next get correct value from title ?
    Last edited by Kamil027; 05-16-2012 at 06:26 PM.

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    905
    Thanks
    0
    Thanked 118 Times in 117 Posts
    Sorry! My wrong!
    In the original code the ' s in this part, is treated as string limiters in he passed code, though they were intended as characters.
    So what is actually needed is that ' is replaced by '" to be treated as a character.

    Code:
    OnClick='javascript:test(\"" + title + "\")'>"
    Last edited by Lerura; 05-16-2012 at 07:11 PM.

  • #5
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx a lot ! It works like a charm


  •  

    Posting Permissions

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