...

View Full Version : Problem with value



Kamil027
05-16-2012, 06:02 PM
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.

Lerura
05-16-2012, 06:50 PM
<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 \'

Kamil027
05-16-2012, 07:16 PM
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 ?

Lerura
05-16-2012, 07:44 PM
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.


OnClick='javascript:test(\"" + title + "\")'>"

Kamil027
05-16-2012, 08:01 PM
Thanx a lot ! It works like a charm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum