Class ('.class')
S2C Home « Selectors « Class ('.class')
Class selector.
Shorthand version $('.class')
Description
The ('.class')
selector, selects all elements that have the specified class.
- An element can have multiple classes but we only need to match on one of them.
- This method calls JavaScript's
getElementsByClassName()
function 'under the hood' to return the appropriate elements if the browser supports it.
Syntax
Signature | Description |
---|---|
jQuery('.class') | Class match |
Parameters
Parameter | Description |
---|---|
class | A class value to search for. An element can have multiple classes but we only need to match on one of them. |
Return
N/A.
Class ('.class')
ExamplesTop
Selects all elements that have the specified class.
The following example will select any element with a class of 'backGreen' and change the background color to green.
$(function(){
$('#btn1').on('click', function() {
$('.backGreen').css('backgroundColor', 'green');
});
});
The following example will select any element with a class of 'js' (code examples) and change the background colour to yellow.
$(function(){
$('#btn2').on('click', function() {
$('.js').css('backgroundColor', 'yellow');
});
});
The following example will select any element with a class of 'copyright' (copyright title in bottom bar) and change the colour to orange.
$(function(){
$('#btn3').on('click', function() {
$('.copyright').css('color', 'orange');
});
});