Re: shortcut for document.getElementById and some error



HopfZ wrote on 30 jun 2006 in comp.lang.javascript:

Evertjan. wrote:

r = EBI3['myText']

or:

r = EBI3.myText


Both returns null (without error).

You are right, my mistake.

getElementById needs an (argument)


--------- test.html ----------------
<html>
<head>
<title>test</title>
<script language="javascript">

use <script type='text/javascript'>

function load(){
function assert(b,s){ if(!b) alert('false is\n'+s);}

function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;
document.EBI4 = document.getElementById;

assert(EBI2('myText'),'EBI2 works');
assert(EBI3['myText']==null,'EBI3["myText"] returns null');

Do not test for null, test for existence. see below.

assert(EBI3.myText==null,'EBI3.myText returns null');
assert(document.EBI4.myText==null, 'document.EBI4 works');

document.EBI4.myText does not work too !!!!!


alert('end of load');
}
</script>
</head>
<body onload="load();">
<form id="myForm">
<input type="text" id="myText"></input>

</input> is not html

</form>
</body>
</html>

Try this:

<html>
<head>
<script type='text/javascript'>
function load(){
function assert(b,s){ if(!b) alert(s);}

function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;
document.EBI4 = document.getElementById;

assert(EBI2('myText'),'EBI2 error'); // OK SKIPS
assert(EBI3['myText'],'EBI3["myText"] error'); // ERRORS
assert(EBI3.myText,'EBI3.myText error'); // ERRORS
assert(document.EBI4.myText, 'document.EBI4 error'); // ERRORS

alert('end of load');
}
</script>
</head>
<body onload="load();">
<form>
<input type="text" id="myText">
</form>
</body>
</html>




--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
.



Relevant Pages

  • Re: shortcut for document.getElementById and some error
    ... Evertjan. ... var EBI3 = document.getElementById; ...
    (comp.lang.javascript)
  • Re: newbie: css in every page in app
    ... Then I needed to add CSS links to each "head" ... within the html tags. ... code your child to add controls to these containers. ... you get control over both the html head and body. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Converting Perl Web Report to Python
    ... by allowing the user to pass in an SQL query parameter to the sql ...
    (comp.lang.python)
  • Re: Pulse Head
    ... (Thanks a lot, Tek.) ... A fast logic gate or LVDS receiver, ... What's with the html? ...
    (sci.electronics.design)
  • Re: How to center the web page in the screen?
    ... A valid html document is structured like this: ... or a tag pair which I won't go into here. ... suggest further reading elsewhere to see what is allowed within the <head> ... The fix to center pages using publisher 2003 and 2007 ...
    (microsoft.public.publisher.webdesign)