Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,446
    Thanks
    13
    Thanked 361 Times in 357 Posts
    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)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •