[Javascript] onclick and functions

Troy III Ajnej trojani2000 at hotmail.com
Sat Aug 12 12:22:44 CDT 2006


> Hi gang: > 1. Why do the alerts show at start-up?
That's because with: a.onclick = test1(1)
You are assigning the complete body function to the a.onclick (!treated as system "undefined" something)
it's the same as if you've writen:
test1(c) { var d = c + 1; alert(d);
equals anonymous load-time function only that in this case; nowhere to assign c=1.
 
> 2. Why does the onclick not work (i.e., no alerts)?
Because it's not associated with the HTMLobject.event  > What I simply want to do is to call a function (while passing a > variable) by clicking a div.At my knowledge, it  can be done only through inline event function calls, that is:
<div id="test1" onclick="test(1,2,3)"> This is test 1 </div>while the script could go like:
function test1(one,two,three) { var d = two + 1; alert(d); } 
***some inline comments******
> html --> > <div id="test1">> This is test 1> </div>> > <div id="test2">> This is test 2> </div>> > > js ---> > <script type="text/javascript">> > var a =  document.getElementById("test1");> var b =  document.getElementById("test2");> > function test1(c)//In Netscape Browsers this will assign the onclick event to to the c variable;
                           // ie will treat it as undeclared empty var 
> {> var d = c + 1;//You are adding 1(number) to click event object; in ie you are adding NaN +1> alert(d);    //will result click event 1 ; in ie NaN> }> > function test2()> {> var d = 1; //correctly  assigned, value of d var is 1> alert(d);// will alert 1> }> 
> a.onclick = test1(1);//loadtime c: will alert (2) in 1 will be assigned to c; In ie same but immediately followed by error> b.onclick = test2();//correctly will alert (1); but ie has stopped load time exec after that error, no result will alert.> > </script>
 
 
 
Or something > -- > -------~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                         Troy III                           progressive art enterprise~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_________________________________________________________________
Try Live.com: where your online world comes together - with news, sports, weather, and much more.
http://www.live.com/getstarted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20060812/7d2a8981/attachment.htm>


More information about the Javascript mailing list