How to add / remove CSS class dynamically

Total Post:104

Points:728

 2326  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. 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>

      Modified On Mar-30-2018 12:11:14 AM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.