top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to change the class of the selected HTML element using jQuery?

0 votes
525 views
How to change the class of the selected HTML element using jQuery?
posted Aug 5, 2014 by Amritpal Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Consider the following html

<button id="button" click="changeClass();">Click Me to change class of  div</button>
<div id="firstDIv" class="first">This is a div</div>

Now when you click on button,changeClass javascript function is called in that function you can write the following line to change class of a div

$('#firstDiv').removeClass('first').addClass('second');

If you want to toggle the class you can use toggle function of Jquery.

answer Aug 7, 2014 by Naveen Kumar Mukka
0 votes

You can remove and add classes to your selected elements on some events via using removeClass and addClass function of jquery. Here is an Example:


$(document).ready(function(){
$("button").click(function(){
$("h1").removeClass("blue").addClass("important");
});
});


.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}

Heading 1

answer Aug 7, 2014 by Karamjeet Singh
...