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

  • SEARCH
  • Home
  • 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 3496850
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:17:59+00:00 2026-05-18T12:17:59+00:00

I am trying to add a mouseup, mousedown, hover event on several different images

  • 0

I am trying to add a mouseup, mousedown, hover event on several different images – the only problem is that the event only occurs on the first image,
tried using the each() function does not seem to be working.
Any suggestions on how I can do this?

    $(function() {

var filename = $('.imgover').attr('alt');

$('.rolloverimg').each(function(){
   $('#'+ filename).mouseup(function(){
      $(this).children("img").attr('src', 'content/images/buttons/'+ filename + '_up.png' );
    }).mousedown(function(){
      $(this).children("img").attr('src','content/images/buttons/' + filename + '_down.png');
    });

    $('#'+ filename).hover(
      function () {
        $(this).children("img").attr('src', 'content/images/buttons/'+ filename + '_hover.png');
      },
      function () {
        $(this).children("img").attr('src', 'content/images/buttons/' + filename + '_up.png');
      }
    );
});

});

<div class="hdr-btns rolloverimg">
      <a href="#"><img src="content/images/buttons/play_up.png" alt="play" id="play"  class="imgover" /></a>
      <a href="#"><img src="content/images/buttons/register_up.png" alt="register" id="register"  class="imgover" /></a>
</div>
  • 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-18T12:18:00+00:00Added an answer on May 18, 2026 at 12:18 pm

    There is no need to implement a loop in order to add events to multiple elements using jQuery. You can simply apply the events to a selector that selects all the elements that you need.

    For example, the following code adds MouseUp and MouseDown events to all the img tags inside an element that has the rolloverimg class:

    $('.rolloverimg img').mouseup(function () {
        alert('up')
    }).mousedown(function () {
        alert('down');
    });
    

    And if you want to quick test it, here is the full working example that was created starting from your source code:

    <html>
    <head>
        <title></title>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js"></script>
        <script type="text/javascript">
        $(function () {
            $('.rolloverimg img').mouseup(function () {
                alert('up')
            }).mousedown(function () {
                alert('down');
            });
        });
        </script>
    </head>
    <body>
        <div class="hdr-btns rolloverimg">
            <a href="#"><img src="content/images/buttons/play_up.png" alt="play" id="play" class="imgover" /></a>
            <a href="#"><img src="content/images/buttons/register_up.png" alt="register" id="register" class="imgover" /></a>
            <input type="button" value="text" />
        </div>
    </body>
    </html>
    

    Additional info update
    If you do not want to select all the img tags from the div, but rather just the ones that have the imgover class applied to them, you can use the following selector:

    $(function () {
        $('.rolloverimg img.imgover').mouseup(function () {
            alert('up')
        }).mousedown(function () {
            alert('down');
        });
    });
    

    Additional info update2
    You can access the currently selected element using $(this). For example:

    $('.rolloverimg img.imgover').mouseup(function () {
        alert($(this).attr('id') + '_up')
    }).mousedown(function () {
        alert($(this).attr('id') + '_down');
    });
    

    Please let me know if the above helps or if you need more specific help.

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

Sidebar

Related Questions

Am trying to add different numbers from different textbox, in a way that once
Trying to add some different filters (in addition to the ForeignKey filter) to a
Im trying to add an constructor to my webservice, that looks like this: public
I have a custom class MDRect that i am trying to add an NSMutableArray
I am trying add two columns in a text file that are seperated by
Im trying to add a button in my grid view that will redirect me
I am trying to add a scrollbar, or prefrably a slider, that zooms in
How do you associate a -mouseUp: event with the -add: method of a NSArrayController?
I'm trying add a directory of jar files (or barring that, each jar file
Trying to add my event handler to ComponentDispatcher.ThreadPreprocessMessage event like this: using System; using

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.