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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:39:25+00:00 2026-05-27T17:39:25+00:00

I am trying to create some basic button rollover functionality using Jquery and toggleClass.

  • 0

I am trying to create some basic button rollover functionality using Jquery and toggleClass. I am building a list of elements by cloning a DIV from my HTML and duplicating it multiple times (its populating a list of data from a database). To do this I am using a for loop. Here is the currently working code.

            var displayNode = document.getElementById('phoneDisplayContainer');         
        for(var i=0; i<length; i++) {
            //Clone the original container display.
            var clonedDisplay = displayNode.cloneNode(true);
            clonedDisplay.setAttribute('id', 'phoneDisplayContainer' + i);
            //Remove hidden class from cloned Element. NOT CROSS BROWSER!
            clonedDisplay.classList.remove('hidden');

            var children = clonedDisplay.getElementsByTagName('div');
            //Fill new nodes children containers with data.
            children[1].innerHTML = contact.phone[i].type;
            children[2].innerHTML = contact.phone[i].number;
            children[3].setAttribute('onclick', 'PhoneUtility.edit(' + i + ');');
            children[3].setAttribute('id', 'phoneEditDisplay' + i);
            children[4].setAttribute('onclick', 'PhoneUtility.remove(' + i + ');');

            //Hidden elements
            var hidden = new Array(children[3], children[4]);

            //Set rollover events.
            clonedDisplay.setAttribute('onmouseover', '$("#' + children[3].id + '").toggleClass("hidden");');
            clonedDisplay.setAttribute('onmouseout', '$("#' + children[3].id + '").toggleClass("hidden");');

            //Append the new node to the display container
            phoneContainer.appendChild(clonedDisplay);
        }
    }

Is there a way to use Jquery event listeners instead of having to set onmouseover and onmouseout directly on the element?

I tried this:

$(clonedDisplay).mouseover(function() {
    $(children[3]).toggleClass('hidden');
});

With no luck. It just displays performs the rollover effect on the last element in the list. This is actually my first attempt at using jQuery so any other suggestions to ways I could jQuery inside the code would be helpful too.

EDIT: I’d also like to toggle multiple children from the arraylist mentioned in the for loop. How would I set this up? I can’t seem to pass an array to the jquery command without getting errors.

  • 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-27T17:39:26+00:00Added an answer on May 27, 2026 at 5:39 pm

    The following code after your for loop should let you assign all the mouseover and mouseout handlers in one go to apply to all the clones:

    $('div[id^="phoneDisplayContainer"]').mouseover(function() {
       $(this).find("div").eq(3).toggleClass("hidden");
    }).mouseout(function() {
       $(this).find("div").eq(3).toggleClass("hidden");
    });
    
    // or, given that both handlers do the same thing:
    $('div[id^="phoneDisplayContainer"]').on("mouseover mouseout", function() {
       $(this).find("div").eq(3).toggleClass("hidden");
    })
    

    (If you’re using jQuery older than 1.7 use .bind() instead of .on().)

    The above says to find all the divs with an id beginning with “phoneDisplayContainer” and assign event handlers. Within the handler, find the fourth descendant div and toggle the class.

    You don’t show your HTML or CSS, but you could do this all in your CSS if you like. Assuming you can assign a common class (“parentDiv”) to the divs that you want to trap the hover event on, and a common class (“childDiv”) to their child div (the one to be hidden), you can do this:

    .parentDiv .childDiv { visibility: hidden; }
    .parentDiv:hover .childDiv { visibility: visible; }
    

    (Obviously you can give more meaningful class names to fit your structure.)

    Otherwise, again if you can assign those classes to the appropriate divs then after your loop you can do this with jQuery:

    $(".parentDiv").on("mouseover mouseout", function() {
       $(this).find(".childDiv").toggleClass('hidden');
    });
    

    Basically the same as what I said initially, but using classes for selectors. If you feel like I’m pushing a class-based solution on you that’s because I am: it tends to make this sort of thing much easier.

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

Sidebar

Related Questions

I'm trying to implement the WMD editor used on StackOverflow to create some basic
I'm trying to create a page that tracks some basic user statistics in a
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
I'm trying to create some sort of many-to-one association. The basic premise is a
I am trying to create a ValueProxy which holds some basic information about a
I'm trying to create some HtmlHelper extensions and ran into a bit of a
I am trying to create some reusable components in Silverlight2 . The difficulty comes
I am trying to create some charts of data (eg http://www.amibroker.com/ ). Is there
I am trying to create some rules and facts on a certain situation. The
I am trying to create some global variables in a firefox extension. In my

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.