Hello and welcome to our community! Is this your first visit?
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
    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:
    <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?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 387 Times in 383 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