Choose a location:
posted 6/3/2010 by Raghav Khunger
In this blog I will show you how to remove all the classes from a control with jQuery. Few days back a person on forums asked the same question and so I decided to blog for it. In this example I have used a div which is associated with two classes '.notice' and '.warning' , there is an input button too. On clicking the input button the classes of that div will be removed. Below is the source code for it.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <style type="text/css"> .notice { font-size: large; font-weight: bold; } .warning { color: Red; } </style> </head> <body> <form id="form1" runat="server"> <div id="mydiv" class="warning notice"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book </div> <input id="Button1" type="button" value="Test" /> </form> <script type="text/javascript"> $('#Button1').click(function () { $('#mydiv').removeClass(); }); </script> </body> </html>
Above the heart of the code which is used to remove the classes is
$('#mydiv').removeClass()
It will remove all the classes linked to that selector. Further if you want to remove a particular class you can go: for $('#mydiv').removeClass('className')
Do let me know your feedback, comments
What kind of email newsletter would you prefer to receive from CodeAsp.Net? 18