Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:20:09+00:00 2026-05-26T17:20:09+00:00

When I access <div> or <p> elements by class with jQuery for a click

  • 0

When I access <div> or <p> elements by class with jQuery for a click function, it repeats the event by how many elements are in the array or stack. So, if I have 3 <div> elements on top of each other or next to each other, the one on the bottom, or the one to the right, will go through the event once and the one on the top or the left will go through the event 3 times.

Am I doing something wrong? Or is this not meant to be done with jQuery?

[revision]
sorry if i worded this in a confusing way. here is a link… you will better understand my problem there. just add a couple new elements via the form and click on them.

http://jsfiddle.net/rNj6e/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:20:09+00:00Added an answer on May 26, 2026 at 5:20 pm

    Now that you’ve posted a fiddle showing the problem, I can actually answer. The problem is that you bind the click event handler to .dp inside the click event handler bound to #add. So what happens is this:

    1. You fill in the form and click #add, which creates a new element with class dp and appends it
    2. It then binds a click event handler to every element with class dp (there’s only 1, the 1 we just added)
    3. You fill in the form again, click #add, which repeats steps 1 and 2, so it binds another click event listener to the first .dp element, and binds one to the new element.
    4. Repeat as necessary, binding more and more event handlers to the existing elements every time you click #add!

    To fix this, you need to bind the event handler to .dp outside of the #add event handler. The problem is that you’re creating new .dp elements on the fly, so just using .click won’t bind to elements that are not in the DOM yet. To solve that, you can use delegate, which binds an event handler to elements matching the selector now and in the future:

    $("#preview").delegate(".dp", "click", function(event){
        alert(this.id);
    });
    

    Here’s an updated fiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation: <nav id=access role=navigation> <div class=menu> <ul> <li class=page_item><a href=#pricing
I have the following HTML in a JSP file: <div class=custList> <table class=dataGrid> <c:forEach
I have a parent Div : <div id=ctl00_MainContentAreaPlaceHolder_deliveryOpen class=delivery_open style=display: block;> //Child select Element
I have an image that is wrapped inside a couple of DIV elements <div
I have code to create another row (div with inputs) on a button click.
How to access the div with specific class that is on the same level
I have a DIV block with 3 DIV elements: upvote, votes, and downvote. When
i've got an array like following [jQuery('div#example1'), jQuery('div#example2'), jQuery('div#example3'), jQuery('div#etcetera')] Goal elementArray = [jQuery('div#example1'),
I have the following div element <div id=row_1 class=active_page workorders_edit></div> And I want to
I have a small question concerning css styles and javascript / jquery. Lets say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.