Wednesday, 27 August 2008

Tuesday, 26 August 2008

Thursday, 21 August 2008

JavaScript getElementById Issues

Aug. 21, 2008

One of my absolute favorite parts (other than Alex's foreword) of Matthew Russell's Dojo: The Definitive Guide is a warning in the Toolkit Overview chapter that reads:

"Viewing an API call as 'worthless' may be an indication that you may be confused about the exact utility that the call provides. Whenever this happens, review the documentation to find out what it is you're missing. If the documentation still doesn't convince you, hop on a mailing list or IRC channel and ask someone about it."

One of the reasons he mentions this is that he's just covered Dojo's dojo.byId function. While the function is shorter and easier to remember than document.getElementById it also smooths out a browser inconsistency. Shockingly, this inconsistency happens in Internet Explorer.

JavaScript toolkits fix this by doing a couple of clever things. FIrst, you can just use document.getElementById to get the element. You then do a lookup to make sure the returned node really has that id. You can't simply do a node.id check though, since if the returned element is a form, node.id might return an input node within that form (eg a text input field with name="id"). Your check, then, should be (node.id.value == id). If the node you received through document.getElementById doesn't pass this test, then use document.all[id] to get an array of nodes, and look through these for the first one that passes the test.

Wednesday, 13 August 2008

Monday, 11 August 2008

Monday, 4 August 2008