1

I have a page where I have many controls and then I have a div element and have 10 asp buttons in that div element.

I want to bind mousehover event to only those buttons which are inside the div element.

Currently i m doing it like:

<script src="jquery-1.6.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#MangoPager_btn1").hover(mousehoverfunction);
            $("#MangoPager_btn2").hover(mousehoverfunction);
            $("#MangoPager_btn3").hover(mousehoverfunction);
            $("#MangoPager_btn4").hover(mousehoverfunction);
            $("#MangoPager_btn5").hover(mousehoverfunction);
            $("#MangoPager_btn6").hover(mousehoverfunction);
            $("#MangoPager_btn7").hover(mousehoverfunction);
            $("#MangoPager_btn8").hover(mousehoverfunction);
            $("#MangoPager_btn9").hover(mousehoverfunction);
            $("#MangoPager_btn10").hover(mousehoverfunction);
        }); 
</script>

Is there any way in jquery so that i can declare a list of button ids something like [MangoPager_btn10,MangoPager_btn2,MangoPager_btn3] so that i don't have to do that for every single button id.

I tried with input type but then it's applying the function for all input type objects not for buttons which are in the div elemet.

Any other bettrer way to do it in jquery .... I

7 Answers 7

1

Try this:

$(function(){
  $("[id^='MangoPager_btn']").hover(mousehoverfunction);
})
Sign up to request clarification or add additional context in comments.

Comments

1
$('#DivElementId input').hover(mousehoverfunction);

Comments

1

You can simply give a class to the desired inputs:

$(".mouse-over").hover(mousehoverfunction);

or if you just have a few ids:

$("#MangoPager_btn1, #MangoPager_btn2, #MangoPager_btn3").hover(mousehoverfunction);

Comments

0

$("#MangoPager button").hover(mousehoverfunction);

Bind the function to the parent div and then all the buttons like this. Assuming MagoPager is the id of the parent div.

or

$("#MangoPager input[type=]").hover(mousehoverfunction);

Comments

0

try something like this:

$(".containerclass input[type=submit]").hover(mousehoverfunction);

this will bind the function to all buttons inside a container wit (css)class="containerclass"

Comments

0

I recommend using delegate: http://api.jquery.com/delegate/ because it will attach a handler to one or more events for all elements

Comments

0

In this cases I use filters http://api.jquery.com/filter/ with regexp

$("input").filter(function()
{
return /MangoPager_btn/.test(this.id)
}).hover(mousehoverfunction);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.