jQuery add or remove class

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video we will discuss
1. How to check if an element has a css class
2. How to add or remove css classes
3. How to toggle css classes

hasClass - Returns true if an element has the specified class otherwise false
addClass - Adds one or more specified classes. To add multiple classes separate them with a space.
removeClass - Removes one or multiple or all classes. To remove multiple classes separate them with a space. To remove all classes, don't specify any class name.
toggleClass - Toggles one or more specified classes. If the element has the specified class then it is removed, if the class is not present then it is added.

Replace < with LESSTHAN symbol and > with GREATERTHAN symbol

<html>
<head>
<title></title>
<style>
.boldClass {
font-weight: bold;
}
.italicsClass {
font-style: italic;
}
.colorClass {
color: red;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
$('#btn1').click(function () {
$('p').addClass('colorClass');
});

$('#btn2').click(function () {
$('p').removeClass('colorClass');
});

$('#btn3').click(function () {
$('p').addClass('colorClass italicsClass');
});

$('#btn4').click(function () {
$('p').removeClass('colorClass italicsClass');
});

$('#btn5').click(function () {
$('p').addClass('colorClass italicsClass boldClass');
});

$('#btn6').click(function () {
$('p').removeClass();
});
});
</script>
</head>
<body style="font-family:Arial">
<p>Pragim Technologies</p>
<table>
<tr>
<td>
<input id="btn1" style="width:250px" type="button"
value="Add Color Class" />
</td>
<td>
<input id="btn2" style="width:250px" type="button"
value="Remove Color Class" />
</td>
</tr>
<tr>
<td>
<input id="btn3" style="width:250px" type="button"
value="Add Color and Italics Classes" />
</td>
<td>
<input id="btn4" style="width:250px" type="button"
value="Remove Color and Italics Classes" />
</td>
</tr>
<tr>
<td>
<input id="btn5" style="width:250px" type="button"
value="Add Color, Italics & Bold Classes" />
</td>
<td>
<input id="btn6" style="width:250px" type="button"
value="Remove All Classes" />
</td>
</tr>
</table>
</body>
</html>
Рекомендации по теме
Комментарии
Автор

thank you for the videos.. do u have java videos???

kaRmajchakma
Автор

JQuery still helping us solve our problems efficiently.... hail this framework everyone! and thanks for the video

ldkqcoq
Автор

u put a lot of effort to make these tutorials... thanks a lot... very hardworking ... really appreciate it. hope more will come ...

rijuchowdhury
Автор

dude, I really like your tutorials.. thank you!!!

Dannyfrsh
Автор

Very nice video. Waiting for more exclusive!

sodiumchloride
Автор

Hey, thank you, your videos are very helpful, easy to understand....Thanks.

anjalikale
Автор

what about ---->hasClass<---- example

Mahmudulhasan-tshm
Автор

Great job..keep the good work..any video planned for angular js thanks once again for your good job..

sanjeevdutta
Автор

Thank you Sir. Is there any CSS or CSS3 tutorial?

boi-huela
Автор

why use each()? I don't use it, but it still work.


$('input').focus(function(){
$(this).addClass('color')
})


$(this).removeClass('color')
})
});

zoeliu
Автор

thank you, your video is helpful.

I think that the below code is better, not (chain instant of each)?

() {

});

() {

});

byair