views:

15458

answers:

9

I'm sure there are a million posts about this out there, but surprisingly I'm having trouble finding something.

I have a simple script where I want to set the onClick handler for an <A> link on initialization of the page.

When I run this I immediately get a 'foo' alert box where I expected to only get an alert when I click on the link.

What stupid thing am I doing wrong? (I've tried click= and onClick=)...

<script language="javascript">

    function init(){

     document.getElementById("foo").click = new function() { alert('foo'); };
    }

</script>

<body onload="init()">
    <a id="foo" href=#>Click to run foo</a>
</body>
+16  A: 

Try:

document.getElementById("foo").onclick = function (){alert('foo');};
CMS
thanks! stupid case sensitivity and 'New'
Simon_Weaver
I didn't realize that new would call the function on page init. Good to know.
discorax
@discorax neither did i. in fact not until just now! a year later
Simon_Weaver
i definitely would not use any getElementbyId stuff anymore. come a LONG way in my javascript experience in the past year. definitely recommend jQuery - or some other such framework. getElementById is just out of date now. see @darryl's answer below
Simon_Weaver
+6  A: 

jQuery:

$('#foo').click(function() { alert('foo'); });

Or if you don't want it to follow the link href:

$('#foo').click(function() { alert('foo'); return false; });
Darryl Hein
You're missing a trailing ); in both of those.
Xiong Chiamiov
Ops! thxs
Darryl Hein
should be wrapped in $(function() { ... }; so that it runs (and looks in the dom for 'foo' only after the DOM is complete)
Simon_Weaver
This DOES NOT Chante the link only but call it. $('#foo').onclick= is the good way to do it.
Daok
+16  A: 

Use .onclick (all lowercase). Like so:

document.getElementById("foo").onclick = function () {
  alert('foo'); // do your stuff
  return false; // <-- to suppress the default link behaviour
};

Actually, you'll probably find yourself way better off using some good library (I recommend jQuery for several reasons) to get you up and running, and writing clean javascript.

Cross-browser (in)compatibilities are a right hell to deal with for anyone - let alone someone who's just starting.

Már Örlygsson
'return false;' is an important addition, IMHO.
Richard A
Yup, for newcomers, return false is completely non-obvious.
Már Örlygsson
A: 

I'd try this in jQuery:

$('#foo').attr("onclick", "javascript: alert('foo');");

dhulk
I don't know for sure, but I don't believe this will work (javascript:...).
Darryl Hein
It may work (I didn't test it) but it's very poorly written code :(
Zack Mulgrew
+1 Didn't address the OP question but answered mine!
Andrew
Holy cow, what was I thinking?
dhulk
+1  A: 

I agree that using jQuery is the best option. You should also avoid using body's onload function and use jQuery's ready function instead. As for the event listeners, they should be functions that take one argument:

document.getElementById("foo").onclick = function (event){alert('foo');};

or in jQuery:

$('#foo').click(function(event) { alert('foo'); }
kgiannakakis
+1  A: 

Here is the YUI counterpart to the jQuery posts above.

<script>
  YAHOO.util.Event.onDOMReady(function() {   
    document.getElementById("foo").onclick =  function (){alert('foo');};
  });
</script>
blak3r
A: 

Hi all,

I tried more or less all above out the other day to dynamically assign onclick action for a button, but I was yet to find a working solution until I tried this one. I deem that it is identical for the link. There it is:

var submitButton = document.getElementById('submitButton'); submitButton.setAttribute('onclick', 'alert("hello");');

Cheers, Selin Ebeci

Selin Ebeci
A: 

Sorry to be an4l about it, but the YUI example above should really be:

<script>
  YAHOO.util.Event.onDOMReady(function() {   
     Dom.get("foo").onclick =  function (){alert('foo');};
  });
</script>
Web Development Guy
A: 

Help please

I am very new to this so please be kind and type very slow so I can understand....

how do I dynamically change an onclick event to a new function here is the example

<img name="space1" id="space1" src="images/8001.jpg" height="150 px" onclick="lrgWithInfo('1')" />

here is what isnt working all I want to change is the value passed.

document.getElementById("space1").onclick = lrgWithInfo('13');

Thanks for your help

kim

kim