View Full Version : how to start a function when click a memeber of a class

03-27-2012, 06:02 AM
I have a question: look at the following html code:
<div class='class1'>text1</div>
<div class='class1'>text2</div>
<div class='class1'>text3</div>
If I want to change the background color of the textn by clicking it, what should I do instead of inserting "onclick='someFunction()'" in every div?

03-27-2012, 07:48 AM
there are several possibilities.
- attach the event to the parent element and execute the code on the target element (Event.target).
- attach the event to each <div> in JavaScript (assign inside a loop)