Chris
Chris

Reputation: 3129

How to use jquery to change button states over multiple images

I was wondering what is the proper way of using jquery to change HyperLinkImage States, i.e mousedown, onmouseup etc...other than copying and pasting the same function and changing id names? Here is the code that I was just going to copy and paste and change the clientid parts to reflect 5 other image links

$(function () {

        $("#<%=HyperLink1.ClientID%> img").hover(function () {
            $(this).attr("src", "/HoverIcons/Icon_lab.png");
        }, function () {
            $(this).attr("src", "/ActiveIcons/Icon_lab_a.png");
        }).mousedown(function () {
            $(this).attr("src", "/DownIcons/iconlab.png");
        }).mouseup(function () {
            $(this).attr("src", "/ActiveIcons/Icon_lab_a.png");
        });
    });

Is there a cleaner way to do this?

Here is the part where all the links are in my markup..

<div id="Navigation1">
            <div id="Nav">
                <table style="width: 100%;">
                    <tr>
                        <td>
                            <asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="~/ActiveIcons/Icon_lab_a.png" NavigateUrl="~/Tabbet.aspx">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <asp:HyperLink ID="HyperLink2" runat="server" ImageUrl="~/ActiveIcons/Icon_ABC_a.png">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <asp:HyperLink ID="HyperLink3" runat="server" ImageUrl="~/ActiveIcons/Icon_math_a.png">HyperLink</asp:HyperLink>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:HyperLink ID="HyperLink4" runat="server" ImageUrl="~/ActiveIcons/Icon_english_a.png">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <asp:HyperLink ID="HyperLink5" runat="server" ImageUrl="~/ActiveIcons/Icon_history_a.png">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <asp:HyperLink ID="HyperLink6" runat="server" ImageUrl="~/ActiveIcons/Icon_geography_a.png">HyperLink</asp:HyperLink>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:HyperLink ID="HyperLink7" runat="server" ImageUrl="~/ActiveIcons/Icon_cyberspace_a.png">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <img alt="" class="auto-style3" src="ActiveIcons/New_subject_coming.png" /></td>
                        <td>
                            <img alt="" class="auto-style3" src="ActiveIcons/New_subject_coming.png" /></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td>

                            &nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </div>

I'm just starting to change the code now, this is my layout for the images.

Upvotes: 0

Views: 116

Answers (2)

arvic.rivera
arvic.rivera

Reputation: 673

i found this answer in other question:

<script src="Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {

        $("#<%=Hyperlink1.ClientID%> img").hover(function () {
            $(this).attr("src", "/Images/logo.png");
        }, function () {
            $(this).attr("src", "/Icons/Icon_english_a.png");
        }).mousedown(function () {
            $(this).attr("src", "/Images/logo.png");
        }).mouseup(function () {
            $(this).attr("src", "/Icons/Icon_english_a.png");
        });
    });

</script>

Reference: hyperlink not being able to have image states, i.e onmousedown

UPDATE

<table>
    <tr>
        <td class="nav">
            <table>
                <tr>
                    <td>
                        <asp:HyperLink ID="HyperLink1" CssClass="lab" runat="server" ImageUrl="~/ActiveIcons/Icon_lab_a.png" NavigateUrl="~/Tabbet.aspx">HyperLink</asp:HyperLink>
                    </td>
                    <td>
                        <asp:HyperLink ID="HyperLink2" CssClass="abc" runat="server" ImageUrl="~/ActiveIcons/Icon_ABC_a.png">HyperLink</asp:HyperLink>
                    </td>
                    <td>
                        <asp:HyperLink ID="HyperLink3" CssClass="math" runat="server" ImageUrl="~/ActiveIcons/Icon_math_a.png">HyperLink</asp:HyperLink>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
<table>
<script type="text/javascript">
$(document).ready(function() {
  $('.nav a img').hover(function () {
      if($(this).parent().hasClass("lab"))
        $(this).attr("src", "/Images/lab_hover.png");
      //if hasClass("abc")
      //if hasClass("math")
  }, function () {
      if($(this).parent().hasClass("lab"))
            $(this).attr("src", "/Images/lab_a.png");
      //if hasClass("abc")
      //if hasClass("math")
  }).mousedown(function () {
    if($(this).parent().hasClass("lab"))
      $(this).attr("src", "/Images/lab_mousedown.png");
    //if hasClass("abc")
    //if hasClass("math")
  }).mouseup(function () {
    if($(this).parent().hasClass("lab"))
      $(this).attr("src", "/Images/lab_mouseup.png");
    //if hasClass("abc")
    //if hasClass("math")
  });
});
</script>

I hope this helps you.

Upvotes: 1

Bhavesh Kachhadiya
Bhavesh Kachhadiya

Reputation: 3962

Made this change in your .aspx file.

Add CssClass="linkcssclass" to your hyperlinks as follow:

<div id="Navigation1">
            <div id="Nav">
                <table style="width: 100%;">
                    <tr>
                        <td>
                            <asp:HyperLink ID="HyperLink1" CssClass="linkcssclass" runat="server" ImageUrl="~/ActiveIcons/Icon_lab_a.png" NavigateUrl="~/Tabbet.aspx">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <asp:HyperLink ID="HyperLink2" CssClass="linkcssclass" runat="server" ImageUrl="~/ActiveIcons/Icon_ABC_a.png">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <asp:HyperLink ID="HyperLink3" CssClass="linkcssclass" runat="server" ImageUrl="~/ActiveIcons/Icon_math_a.png">HyperLink</asp:HyperLink>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:HyperLink ID="HyperLink4" CssClass="linkcssclass" runat="server" ImageUrl="~/ActiveIcons/Icon_english_a.png">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <asp:HyperLink ID="HyperLink5" CssClass="linkcssclass" runat="server" ImageUrl="~/ActiveIcons/Icon_history_a.png">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <asp:HyperLink ID="HyperLink6" CssClass="linkcssclass" runat="server" ImageUrl="~/ActiveIcons/Icon_geography_a.png">HyperLink</asp:HyperLink>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:HyperLink ID="HyperLink7" CssClass="linkcssclass" runat="server" ImageUrl="~/ActiveIcons/Icon_cyberspace_a.png">HyperLink</asp:HyperLink>
                        </td>
                        <td>
                            <img alt="" class="auto-style3" src="ActiveIcons/New_subject_coming.png" /></td>
                        <td>
                            <img alt="" class="auto-style3" src="ActiveIcons/New_subject_coming.png" /></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td>

                            &nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </div>

Make following changes in your jQuery:

<script src="Scripts/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $(".linkcssclass img").each(function (i) {
                $(this).hover(function () {
                       $(this).attr("src", "/HoverIcons/Icon_lab.png");
                }, function () {
                        $(this).attr("src", "/ActiveIcons/Icon_lab_a.png");
                }).mousedown(function () {
                   $(this).attr("src", "/DownIcons/iconlab.png");
                }).mouseup(function () {
                $(this).attr("src", "/ActiveIcons/Icon_lab_a.png");
              });
            });


        });


    </script>

Upvotes: 1

Related Questions