how to make div click-able?

omg picture omg · Jun 29, 2009 · Viewed 213.6k times · Source
<div><span>shanghai</span><span>male</span></div>

For div like above,when mouse on,it should become cursor:pointer,and when clicked,fire a

javascript function,how to do that job?

EDIT: and how to change the background color of div when mouse is on?

EDIT AGAIN:how to make the first span's width=120px?Seems not working in firefox

Answer

James picture James · Jun 29, 2009

Give it an ID like "something", then:

var something = document.getElementById('something');

something.style.cursor = 'pointer';
something.onclick = function() {
    // do something...
};

Changing the background color (as per your updated question):

something.onmouseover = function() {
    this.style.backgroundColor = 'red';
};
something.onmouseout = function() {
    this.style.backgroundColor = '';
};