Prev: option & textnode
Next: iframes? something better?
From: Geoff on 22 Apr 2010 14:52 On Thu, 22 Apr 2010 12:24:50 +0200, Gregor Kofler <usenet(a)gregorkofler.com> wrote: >Am 2010-04-22 08:55, Geoff meinte: >> Hello >> >> I am having a problem with part of my code and think I know why?! >> >> I create a div using >> >> var new1 = document.createElement('div'); >> new1.innerHTML = str1; >> new1.id = 'divlhs' + sectionCount; >> new1.className = 'nav'; >> document.body.appendChild(new1); >> >> and add the str1 content. This works fine but later I get a problem in >> in that new content is not removed when css is used to make the div >> hidden. I think this is because I am using the same id (certainly I am >> using the same sectionCount value) again and ids must be unique. > Gregor, Thanks for your reply. >Indeed. Why are you using ids when you cant ensure that they stay >unique? Use classes instead. I am using class for another property. >> How do I reset the new1.id - if reset is the correct term? > >Erm... > >new1.id = ""; OK! I have been able to get rid of the ids as part of by using for (var cdiv=startValue;cdiv<startValue+tasks_per_set;cdiv++) { if (document.getElementById("divlhs" + cdiv)) { document.body.removeChild(document.getElementById("divlhs" + cdiv)); } if (document.getElementById("divrhs" + cdiv)) { document.body.removeChild(document.getElementById("divrhs" + cdiv)); } } Cheers Geoff > >Gregor
From: Gregor Kofler on 22 Apr 2010 17:37 Am 2010-04-22 20:52, Geoff meinte: > On Thu, 22 Apr 2010 12:24:50 +0200, Gregor Kofler > <usenet(a)gregorkofler.com> wrote: >> Indeed. Why are you using ids when you cant ensure that they stay >> unique? Use classes instead. > > I am using class for another property. You mean you use the class attribute to identify objects in a different context? You can assign several (CSS-)classes to an element. With <input class="foo bar"> getElementsByClassName()[1] (or the custom stand-ins for this method) will find this input both when you look for className "foo" and className "bar". Gregor [1] https://developer.mozilla.org/en/DOM/document.getElementsByClassName -- http://www.gregorkofler.com
From: Gregor Kofler on 22 Apr 2010 17:41 Am 2010-04-22 20:52, Geoff meinte: > On Thu, 22 Apr 2010 12:24:50 +0200, Gregor Kofler > <usenet(a)gregorkofler.com> wrote: [snip] > if (document.getElementById("divlhs" + cdiv)) { > document.body.removeChild(document.getElementById("divlhs" + > cdiv)); A bit more efficient: var foo, body = document.body; .... if((foo = document.getElementById("divlhs" + cdiv))) { body.removeChild(foo); } Gregor -- http://www.gregorkofler.com
From: Thomas 'PointedEars' Lahn on 22 Apr 2010 18:06 Gregor Kofler wrote: > Am 2010-04-22 08:55, Geoff meinte: >> var new1 = document.createElement('div'); >> new1.innerHTML = str1; >> new1.id = 'divlhs' + sectionCount; >> [...] >> How do I reset the new1.id - if reset is the correct term? > > Erm... > > new1.id = ""; That (alone) doesn't strike me as being a particularly good idea. Better: new1.removeAttribute("id"); See also: <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-63534901> PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
From: Geoff on 22 Apr 2010 19:29
On Fri, 23 Apr 2010 00:06:20 +0200, Thomas 'PointedEars' Lahn <PointedEars(a)web.de> wrote: >Gregor Kofler wrote: > >> Am 2010-04-22 08:55, Geoff meinte: >>> var new1 = document.createElement('div'); >>> new1.innerHTML = str1; >>> new1.id = 'divlhs' + sectionCount; >>> [...] >>> How do I reset the new1.id - if reset is the correct term? >> >> Erm... >> >> new1.id = ""; > >That (alone) doesn't strike me as being a particularly good idea. Better: > > new1.removeAttribute("id"); Thomas, Which is the better, more widely supported, new1.id = 'divlhs' + sectionCount; or new1.setAttribute('id','divlhs'+sectionCount); ? Geoff > >See also: <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-63534901> > > >PointedEars |