[Javascript] onclick and functions

Rakesh Pai rakeshpai at gmail.com
Sat Aug 12 09:30:52 CDT 2006


Didn't try this yet, but I'd think the way you are registering the
event is wrong. You cannot pass a function with arguments as a handler
for an event.

Try this instead:

a.onclick = function() { test1(1); }
b.onclick = test2;

I might be wrong though.

That said, using a addEvent function is a cleaner way to attach
handlers to an event. Do a google search for it.

On 8/12/06, tedd <tedd at sperling.com> wrote:
> Hi gang:
>
> Two questions (please be kind):
>
> 1. Why do the alerts show at start-up?
>
> 2. Why does the onclick not work (i.e., no alerts)?
>
> I spent a lot of time trying to find a reference, but couldn't find
> anything regarding this. Everything I read suggested that this should
> work.
>
> What I simply want to do is to call a function (while passing a
> variable) by clicking a div.
>
> I must be doing something very simple, very wrong -- what is it?
>
> Thanks.
>
> tedd
>
> ----
>
> 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)
>         {
>         var d = c + 1;
>         alert(d);
>         }
>
> function test2()
>         {
>         var d = 1;
>         alert(d);
>         }
>
> a.onclick = test1(1);
> b.onclick = test2();
>
> </script>
> --
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>


-- 
Rakesh Pai
Mumbai, India.
rakesh.pai at gmail.com
http://piecesofrakesh.blogspot.com/



More information about the Javascript mailing list