jQuery Flashcards
remove a class
Description: Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
removeClass( [className ] )
removeClass( function(index, class) )
add a class
.addClass( className )
.addClass( function(index, currentClass) )
function is a function returning one or more space-separated class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, this refers to the current element in the set.
set or return the text content of selected elements
.text()
can also return the content of a callback function:
.text(function(x) {return “bob” + x;});
set or return the content of selected elements, including HTML markup
.html()
can also return the content of a callback function:
.html(function(x) {return “<div>” + x + “</div>”;});
set or return the value of form fields
.val()
can also return the content of a callback function:
.val(function(x) {return “bob” + x;});
html vs text vs val
getters and setters
GETTERS
.text returns the combined text contents of all matching elements
.html returns the html content of the first matching element
.val gets the value of the input element, regardless of type
SETTERS
$(“#div1”).html(‘<a>Link</a><b>hello</b>’);
$(“#div2”).text(‘<a>Link</a><b>hello</b>’);
div1 (html) recognizes html elements in a string:
Linkhello //”Link” is underlined, “hello” is in bold
div2 (text) treats everything in a string as text:
<a>Link</a><b>hello</b>
Add or remove one or more classes from each element in the set of matched elements, depending on either the class’s presence or the value of the switch argument.
toggleClass(className, switch);
if className is absent or switch === true, then the className is added. Otherwise, className is removed.