benwells.me.uk

Return by Id

I thought I'd start from the beginning with a function I use lot in javascript. This is very simple call with an id to retrieve and object: function return_by_id(id) {
if (document.getElementById)
var return_var = document.getElementById(id);
else if (document.all)
var return_var = document.all;
else if (document.layers)
var return_var = document.layers;
else return false;
return return_var;
}

Running this code in conjunction with a form for example gives a cross browser solution to retrieving values. Basically it's nice to know that when I call to ask for an object I'll get one back: document.write('<input type="text" id="form_input" value="input" />');
document.write('<br /><input type="button" value="alert" onClick="alert(return_by_id('form_input'))" />');
alert(return_by_id('form_input'));

And that is it in use. The function looks through each possible function most browsers will return values through getByElementId, but if they don't it will get picked up through a call to document array and layer array to find the id.