Home > DeveloperSection > Forums > How to add / remove CSS class dynamically
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    April-16-2013 1:45 AM

 CSS-CSS3 CSS-CSS3 
Ratings:


 1 Reply(s)
 1412  View(s)
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!


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    April-16-2013 8:26 AM

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>

Don't want to miss updates? Please click the below button!

Follow MindStick