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 6538975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:48:03+00:00 2026-05-25T10:48:03+00:00

I’m a newbie to Javascript & HTML5. I’m iterating through a set of objects

  • 0

I’m a newbie to Javascript & HTML5. I’m iterating through a set of objects called requests and creating divs for them. I’m trying to have it so that if any of the items are hovered over the style class changes, and if they are clicked on that I will later invoke a function but for now just want an alert. Only the last item gets it.

I’ve looked at what seemed like similar issues other people have had, but I can’t see where I am going wrong.

        for (i= 0; i<reqs.length; i++) {
            var requestID = "request"+i;
            // Build the DIV for each request
            element.innerHTML += "<div id="+requestID+" class=request><img class=requestImage src=images/"
                +reqs[i].image+" alt=Face />&nbsp;&nbsp;"+reqs[i].name+"</div>";

            var requestElement = $('#'+requestID);
            requestElement.hover(
                function() {
                    $(this).removeClass().addClass("requestHover");
                    },
                function() {
                $(this).removeClass().addClass("request");
                }
            );
            requestElement.click(
            // if the request is clicked, then alert me - testing
                function() {
                    alert('Handler for .click() called.');
                }
            );

        }// end for

From my understanding the $(‘#request1’) should reference the first div item, and $(‘#request2’) the second, etc. It behaves like each .hover and .click assignment overwrites the previous one.

  • 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-25T10:48:04+00:00Added an answer on May 25, 2026 at 10:48 am

    I’d write it this way

        for (i= 0; i<reqs.length; i++) {
            var requestID = "request"+i;
            // Build the DIV for each request
            element.innerHTML += "<div id="+requestID+" class=request><img class=requestImage src=images/"
                +reqs[i].image+" alt=Face />&nbsp;&nbsp;"+reqs[i].name+"</div>";
        }// end for
        var requestElement = $(".request");
        requestElement.hover(
            function() {
                $(this).removeClass().addClass("requestHover");
             },
             function() {
                $(this).removeClass().addClass("request");
             }
        );
        requestElement.click(
          // if the request is clicked, then alert me - testing
          function() {
             alert('Handler for .click() called.');
          }
        );
    

    Edit to answer to your comment:

    No, it’s a matter of closure. At the end of the loop, requestID is always the same, so $("#"+requestID); is always the same. It’s has if there is only one bind.

    To overcome such a thing, you have some option:

    • using $.each to loop (see http://forum.jquery.com/topic/binding-event-to-element-dynamically , http://forum.jquery.com/topic/binding-click-event-in-a-loop, http://api.jquery.com/jQuery.each/ )
    • using the live() method (see http://api.jquery.com/live/, http://jsfiddle.net/VrzUb/1/ (used for the click))
    • assigning the events thanks to a selector common to each object (e.g. using classes, as above)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.