Forum Moderators: open
window.onload=function(){(document.getElementById('s').onchange=function(){beta(1);})();}
While it works great on most pages, their are a couple of pages that don't have in id="s".
On those pages I get an error saying s is null, or not an object.
How might I rewrite that code to still perform exactly the same, BUT, not error out if their is no id="s"?
Thanks
I put alert(o); in the code, to see what I get back on both types of pages. Regardless if id="s" existed or not, null was returned.
This is where id="s" is being defined:
<select id="s" name="s">
<option value="a">...</option>
<option value="b">...</option>
</select>
Is something I can tweak in the JS that would run that portion of the script only if id="s" existed on the page?
var o=document.getElementById('s');
if(o)
{
window.onload=function(){(document.getElementById('s').onchange=function(){beta(1);})();}
}
One solution might be to create another function that checks for the existance of 's'. If 's' exists, then it calls beta(). Put your call to this new function in the body onload.
Also form elements should be located within <form> </form> tags. They often work without it, but behavior can be unpredictable. Each browser deals with it differently.