Prev: What browser doesn't support ajax ?
Next: Mutex
From: nameless on 11 Jan 2010 16:42 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 11 Jan 2010 17:18 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 11 Jan 2010 17:18 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 11 Jan 2010 17:30 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 11 Jan 2010 17:44
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 ' Jeff |