Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <4429325.cM6MFbt01H@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sat, 26 Oct 2013 00:38:07 +0200 User-Agent: KNode/4.10.5 Content-Transfer-Encoding: 7Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <0.2881687aedec8c956bad.20131024024418BST.87li1jbfyl.fsf@bsb.me.uk> <3044571.oA5CmXAGKN@PointedEars.de> <6ca9a5c5-bfaa-48ca-947e-be5fc60fb3b3@googlegroups.com> MIME-Version: 1.0 Lines: 97 NNTP-Posting-Date: 26 Oct 2013 00:38:10 CEST NNTP-Posting-Host: 96fd362d.newsspool1.arcor-online.net X-Trace: DXC=o:mFoZ>M7E[=>bdbdS?M0Yic==]BZ:af^4Fo<]lROoRQnkgeX?EC@@P0E8X5N4L0ERDZm8W4\YJN\YUVHKH;GI^WM4^Y;SHD3Z=bR X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:21734 dhtml wrote: > Thomas 'PointedEars' Lahn wrote: >> Tony Johansson wrote: >> > Many thanks nice solution and it was exactly this that I was looking >> > for. >> > >> > [Top post] >> >> What you are looking for is >> >> - to read on attribute properties in order to get rid of setAttribute(); >> - to read on event bubbling in order to get rid of multiple event >> listeners; > > Yes! > > Valuable advice here. > > The information on closures is useful to learn, and should be considered > here for the OP. And in this particular case, OP, bubbling is more > elegant, so look into that. > > Here is a general example, though there must be better examples online. Plenty. > var imgThumbExp = /-thumb$/; > var imgContainer = document.body; // Replace with closer ancestor The comment should be a multi-line pre-comment to tell it apart of deactivating single-line comments. The comment content is very important here. The same as a short scope chain increases efficiency, so does a short event bubbling route. > imgContainer.onclick = imgContainerClicked; > > function imgContainerClicked(ev) { > var target = ev.target; A wrapper is required to support non-W3C DOM implementations, in particular the MSHTML DOM before version 9, or version 9 in Browser Compatibility Mode. > if(target && imgThumbExp.test(target.id)) { > showBigImage(target.id.replace(imgThumbExp , ""); Passing ID strings *instead of* DOM object references to functions is not a good idea. > } > } > > > It is a lot easier (and more efficient) to use the suffix for the *target* element of the feature instead of the source element. And I would filter for a class name, if necessary the element type before that: /* Replace with closer ancestor */ var imgContainer = document.body; imgContainer.onclick = jsx.dom.createEventListener(imgContainerClicked); function imgContainerClicked (ev) { var target = ev.target; /* A wrapper is required to support non-HTML5 DOM implementations */ if (target.classList.contains("thumb")) { showBigImage(document.getElementById(target.id + "-large")); } } See also: Please trim your quotes, and avoid using the borken Google Groups if possible. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.