From: nameless on
Hi at all. With this code ( below ), I insert in the tag with
"results" as identifier, the data that I retrieve from ajax function
( json structure "data" in below" ). But when "data.books[i].name" is
a string with apostrophes, the function onclick doesn't work !! How
can I resolve this issue ?
Thanks :)


e = document.getElementById('results');
for(i=0; i<data.books.length; i++) {

e.innerHTML += "<li><a href=\"#\" onclick=\"document.getElementById
(\'search-q\').value='" + data.books[i].name + "';\" >" + data.books
[i].name + "</a></li> ";


}
From: Evertjan. on
nameless wrote on 11 jan 2010 in comp.lang.javascript:

> Hi at all. With this code ( below ), I insert in the tag with
> "results" as identifier, the data that I retrieve from ajax function
> ( json structure "data" in below" ). But when "data.books[i].name" is
> a string with apostrophes, the function onclick doesn't work !! How
> can I resolve this issue ?
> Thanks :)
>
>
> e = document.getElementById('results');
> for(i=0; i<data.books.length; i++) {
>
> e.innerHTML += "<li><a href=\"#\" onclick=\"document.getElementById
> (\'search-q\').value='" + data.books[i].name + "';\" >" + data.books
> [i].name + "</a></li> ";
>
>
>}
>

var e = document.getElementById('results');
for(var i=0; i<data.books.length; i++) {
e.innerHTML += '<li><a href="#" onclick="setSearchQ(this)">'
+ data.books[i].name + '</a></li>';
};

function setSearchQ(that) {
document.getElementById('search-q').value = that.innerHTML;
};

[not tested]


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: jeff on
nameless wrote:
> Hi at all. With this code ( below ), I insert in the tag with
> "results" as identifier, the data that I retrieve from ajax function
> ( json structure "data" in below" ). But when "data.books[i].name" is
> a string with apostrophes, the function onclick doesn't work !! How
> can I resolve this issue ?
> Thanks :)
>
>
> e = document.getElementById('results');
> for(i=0; i<data.books.length; i++) {
>
> e.innerHTML += "<li><a href=\"#\" onclick=\"document.getElementById
> (\'search-q\').value='" + data.books[i].name + "';\" >" + data.books
> [i].name + "</a></li> ";
>
>

You can either replace ' with \' in data.books[i].name

or

e.innerHTML += '<li><a href="#" onclick="document.getElementById
> (\'search-q\').value="' + data.books[i].name...

but then you have to escape ". If you look at the html again, you'll
see why you can't have this: ='''

Jeff



> }
From: nameless on
On Jan 11, 11:18 pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net>
wrote:
> nameless wrote on 11 jan 2010 in comp.lang.javascript:
>
>
>
> > Hi at all. With this code ( below ), I insert in the tag with
> > "results" as identifier, the data that I retrieve from ajax function
> > ( json structure "data" in below" ). But when "data.books[i].name" is
> > a string with apostrophes, the function onclick doesn't work !! How
> > can I resolve this issue ?
> > Thanks :)
>
> > e = document.getElementById('results');
> > for(i=0; i<data.books.length; i++) {
>
> > e.innerHTML += "<li><a href=\"#\" onclick=\"document.getElementById
> > (\'search-q\').value='" + data.books[i].name + "';\" >" + data.books
> > [i].name + "</a></li> ";
>
> >}
>
> var e = document.getElementById('results');
> for(var i=0; i<data.books.length; i++) {
>    e.innerHTML += '<li><a href="#" onclick="setSearchQ(this)">'
>    + data.books[i].name + '</a></li>';
>
> };
>
> function setSearchQ(that) {
>    document.getElementById('search-q').value = that.innerHTML;
>
> };
>
> [not tested]
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

now it does works :)
From: jeff on
jeff wrote:
> nameless wrote:
>> Hi at all. With this code ( below ), I insert in the tag with
>> "results" as identifier, the data that I retrieve from ajax function
>> ( json structure "data" in below" ). But when "data.books[i].name" is
>> a string with apostrophes, the function onclick doesn't work !! How
>> can I resolve this issue ?
>> Thanks :)
>>
>>
>> e = document.getElementById('results');
>> for(i=0; i<data.books.length; i++) {
>>
>> e.innerHTML += "<li><a href=\"#\" onclick=\"document.getElementById
>> (\'search-q\').value='" + data.books[i].name + "';\" >" + data.books
>> [i].name + "</a></li> ";
>>
>>
>
> You can either replace ' with \' in data.books[i].name

My mistake, wrong escape.

replace ' with &#39;

Jeff
 |  Next  |  Last
Pages: 1 2 3
Prev: What browser doesn't support ajax ?
Next: Mutex