Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-27-2012, 05:02 AM   PM User | #1
cdzhang
New Coder

 
Join Date: Mar 2012
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
cdzhang is an unknown quantity at this point
how to start a function when click a memeber of a class

I have a question: look at the following html code:
/****html***/
<div class='class1'>text1</div>
<div class='class1'>text2</div>
<div class='class1'>text3</div>
......
/**html***/
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?
cdzhang is offline   Reply With Quote
Old 03-27-2012, 06:48 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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)
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 PM.


Advertisement
Log in to turn off these ads.