HOW TO ADD / REMOVE CSS CLASS DYNAMICALLY

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
 1730  View(s)
Ratings:
Rate this:
Hi Everyone!

I want to add and remove css class dynamically using javascript or jquery in html.

please help me!

Thanks in advance!
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to add / remove CSS class dynamically

    Hi Marcel!

    You can do as below

    <div id="div1" class="getid">       
    </div>

    <div id="div2" class="getid">        
    </div>
    <style type="text/css">
            .setpos
            {
                position: relative;
                z-index: 1;
            }
        </style>
    <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    <script type="text/javascript">
            $(document).ready(function () {
                jQuery(".getid").click(function () {
                    jQuery("#div1").removeClass("setpos");
                    jQuery("#div2").removeClass("setpos");
                    var getid = jQuery(this).attr('id');
                    jQuery("#" + getid).addClass("setpos");
                });
            });
    </script>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!