views:

31

answers:

4
<img id="sun" href="logo.jpg"/>

How do I use JQuery to bind it so that when "sun" gets clicked, something happens? I just want bind onclick to sun.

+1  A: 

I'm assuming "sun" is an element with the id "sun".

$("#sun").click(function(){
  alert("I was clicked!");
});
JacobM
${} should be $()
Lachlan Roche
Lachlan -- you're quite right, thanks. I've updated.
JacobM
+2  A: 

If sun is an id,

$('#sun').click( function(eo) {
    // your code here
});

if sun is a class,

$('.sun').click( function(eo) {
    // your code here
}
sberry2A
A: 

You mean this sun ?

aefxx
A: 

If Sun is an id:

$("#sun").click(function(ev){
    // this refers to the dom element
    alert("I was clicked!");
    return false
});

Perhaps it is class:

$(".sun").click(function(ev){
    // this refers to the dom element
    alert("I was clicked!");
    return false
});

Or maybe a class, and the element may be created well after page load - eg via AJAX or DOM manipulation.

$(".sun").live('click',function(ev){
    // this refers to the dom element
    alert("I was clicked!");
    return false
});

JQuery API references:

Lachlan Roche