Set Attributes – attr()
The jQuery attr()
method is also used to set/change attribute values.
The following example demonstrates how to change (set) the value of the href attribute in a link:
Example
$(“button”).click(function(){
$(“#w3s”).attr(“href”, “https://www.w3schools.com/jquery/”);
});
The attr()
method also allows you to set multiple attributes at the same time.
The following example demonstrates how to set both the href and title attributes at the same time:
Example
$(“button”).click(function(){
$(“#w3s”).attr({
“href” : “https://www.w3schools.com/jquery/”,
“title” : “W3Schools jQuery Tutorial”
});
});
Set Content – text(), html(), and val()
We will use the same three methods from the previous page to set content:
text()
– Sets or returns the text content of selected elementshtml()
– Sets or returns the content of selected elements (including HTML markup)val()
– Sets or returns the value of form fields
The following example demonstrates how to set content with the jQuery text()
, html()
, and val()
methods:
Example
$(“#btn1”).click(function(){
$(“#test1”).text(“Hello world!”);
});
$(“#btn2”).click(function(){
$(“#test2”).html(“<b>Hello world!</b>”);
});
$(“#btn3”).click(function(){
$(“#test3”).val(“Dolly Duck”);
});
Get Content – text(), html(), and val()
Three simple, but useful, jQuery methods for DOM manipulation are:
text()
– Sets or returns the text content of selected elementshtml()
– Sets or returns the content of selected elements (including HTML markup)val()
– Sets or returns the value of form fields
The following example demonstrates how to get content with the jQuery text()
and html()
methods:
Example
$(“#btn1”).click(function(){
alert(“Text: “ + $(“#test”).text());
});
$(“#btn2”).click(function(){
alert(“HTML: “ + $(“#test”).html());
});
The following example demonstrates how to get the value of an input field with the jQuery val()
method:
Example
$(“#btn1”).click(function(){
alert(“Value: “ + $(“#test”).val());
});
Add New HTML Content
We will look at four jQuery methods that are used to add new content:
append()
– Inserts content at the end of the selected elementsprepend()
– Inserts content at the beginning of the selected elementsafter()
– Inserts content after the selected elementsbefore()
– Inserts content before the selected elements
jQuery append() Method
The jQuery append()
method inserts content AT THE END of the selected HTML elements.
Example
$(“p”).append(“Some appended text.”);
jQuery prepend() Method
The jQuery prepend()
method inserts content AT THE BEGINNING of the selected HTML elements.
Example
$(“p”).prepend(“Some prepended text.”);
Remove Elements/Content
To remove elements and content, there are mainly two jQuery methods:
remove()
– Removes the selected element (and its child elements)empty()
– Removes the child elements from the selected element
jQuery remove() Method
The jQuery remove()
method removes the selected element(s) and its child elements.
Example
$(“#div1”).remove();
jQuery empty() Method
The jQuery empty()
method removes the child elements of the selected element(s).
Example
$(“#div1”).empty();
Filter the Elements to be Removed
The jQuery remove()
method also accepts one parameter, which allows you to filter the elements to be removed.
The parameter can be any of the jQuery selector syntaxes.
The following example removes all <p>
elements with class="test"
:
Example
$(“p”).remove(“.test”);
This example removes all <p>
elements with class="test"
or class="demo"
:
Example
$(“p”).remove(“.test, .demo”);
jQuery Manipulating CSS
jQuery has several methods for CSS manipulation. We will look at the following methods:
addClass()
– Adds one or more classes to the selected elementsremoveClass()
– Removes one or more classes from the selected elementstoggleClass()
– Toggles between adding/removing classes from the selected elementscss()
– Sets or returns the style attribute
jQuery addClass() Method
The following example shows how to add class attributes to different elements. Of course you can select multiple elements, when adding classes:
Example
$(“button”).click(function(){
$(“h1, h2, p”).addClass(“blue”);
$(“div”).addClass(“important”);
});
jQuery removeClass() Method
The following example shows how to remove a specific class attribute from different elements:
Example
$(“button”).click(function(){
$(“h1, h2, p”).removeClass(“blue”);
});
jQuery toggleClass() Method
The following example will show how to use the jQuery toggleClass()
method. This method toggles between adding/removing classes from the selected elements:
Example
$(“button”).click(function(){
$(“h1, h2, p”).toggleClass(“blue”);
});
Return a CSS Property
To return the value of a specified CSS property, use the following syntax:
css(“propertyname“);
The following example will return the background-color value of the FIRST matched element:
Example
$(“p”).css(“background-color”);
Set a CSS Property
To set a specified CSS property, use the following syntax:
css(“propertyname“,”value“);
The following example will set the background-color value for ALL matched elements:
Example
$(“p”).css(“background-color”, “yellow”);