Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make div click-able?

<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

like image 815
omg Avatar asked Jun 29 '09 09:06

omg


People also ask

Can a div have an onclick?

We can bind a JavaScript function to a div using the onclick event handler in the HTML or attaching the event handler in JavaScript. Let us refer to the following code in which we attach the event handler to a div element. The div element does not accept any click events by default.

How do you make a whole card clickable?

You can wrap block elements with <a> . It is valid HTML5 and renders your card clickable. You may need to adjust the css rules for the text inside this block however.

How do you make a clickable tag in HTML?

The most important attribute that allows one to make links in HTML is the href attribute of the <a> element. As mentioned before, the href attribute indicated the link's destination. To break the code that helps you make text clickable in HTML and understand it better, <a href=” “> helps one to specify the target.

Can a div be a link?

You can't make the div a link itself, but you can make an <a> tag act as a block , the same behaviour a <div> has. You can then set the width and height on it. However, this doesn't make a 'div' into a link. It makes a link into a block element.


1 Answers

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 = ''; }; 
like image 194
James Avatar answered Oct 01 '22 04:10

James