Friday, 15 April 2011

javascript - error 'document.getelementbyid(...)' is null or not an object -



javascript - error 'document.getelementbyid(...)' is null or not an object -

this question has reply here:

why jquery or dom method such getelementbyid not find element? 6 answers

i created 2 javascript functions.

var a, b; function changeto(a, b) { document.getelementbyid(a).classname = 'editborder'; document.getelementbyid(b).classname = 'editborder'; } function changetoo(a, b) { document.getelementbyid(a).classname = 'editborder_'; document.getelementbyid(b).classname = 'editborder_'; }

they called <asp:linkbutton> 1 argument:

<asp:linkbutton id="linkbutton7" runat="server" onmouseover="changeto('div_master');" onmouseout="changetoo('div_master');" text="edit bg color" commandargument="0" onclick="lnkchangebg_click"> </asp:linkbutton>

it works fine in chrome, in ie6 causes next error:

error 'document.getelementbyid(...)' null or not object

how caused , how can solve it?

your function gets 2 required parameters , you're passing one.

you refactor functions sec parameter wont required, this:

function changetoo(a, b) { document.getelementbyid(a).classname = 'editborder_'; if (typeof b !== "undefined") { document.getelementbyid(b).classname = 'editborder_'; } }

it seems bit clunky utilize 2 different functions seem same. if take 1 step further, refactor whole code this:

function changeclass(a, strclass) { if (typeof === "string") { document.getelementbyid(a).classname = strclass; } else if (typeof === "object" && a.length > 0) { (var id in a) { if (a.hasownproperty(id)) { document.getelementbyid(id).classname = strclass; } } } else { throw new error("invalid argument supplied in changeclass()."); } }

now can do:

<asp:linkbutton id="linkbutton7" runat="server" onmouseover="changeclass('div_master', 'editborder');" onmouseout="changeclass('div_master', 'editborder_');" text="edit bg color" commandargument="0" onclick="lnkchangebg_click"> </asp:linkbutton>

or even:

<asp:linkbutton id="linkbutton7" runat="server" onmouseover="changeclass(['div_master', 'anotherelementid'], 'editborder');" onmouseout="changeclass(['div_master', 'anotherelementid'], 'editborder_');" text="edit bg color" commandargument="0" onclick="lnkchangebg_click"> </asp:linkbutton>

if still doesn't prepare problem, louis lazaris wrote an first-class post avoiding problems .getelementbyid() in net explorer.

javascript internet-explorer-6

No comments:

Post a Comment