Selecting and removing multiple classes in jQuery

If you have multiple ASP.NET Panels or <div> tags in your page with different classes, then here's how to select some classes and leave others using jQuery :


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

<script src="Scripts/jquery-1.3.min.js" 

type="text/javascript">

</script>

<script type="text/javascript">

$(document).ready(function() {

$('.oneClass, .oneMoreClass').

removeClass('notReqClass').hover(

    function() {

        $(this).css({ background: "#C1DAD7" }); 

    },

    function() {

        $(this).css({ background: "#ffffff" }); 

     }

    ); 

});

 

</script>

 

<style type="text/css">

.oneClass

{

    font-style:italic;

}

.oneMoreClass

{

    font-weight:bold;

}

.notReqClass

{

    background:White;

}

 

</style>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Panel ID="Panel1" runat="server" class="oneClass"> 

    Panel 1

</asp:Panel>

<asp:Panel ID="Panel2" runat="server" class="oneMoreClass">

    Panel 2

</asp:Panel>

<asp:Panel ID="Panel3" runat="server" class="oneClass">

    Panel 3

</asp:Panel>

<asp:Panel ID="Panel4" runat="server" class="notReqClass">

    Panel 4

</asp:Panel>

<asp:Panel ID="Panel5" runat="server" class="oneClass">

    Panel 5

</asp:Panel>

</div>

</form>

</body>

</html>



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: