Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery settings


May 07, 2021 jQuery


Table of contents


jQuery - Set content and properties


Set content - text(), html(), and val()

We'll set up the content using three of the same methods in the previous chapter:

  • text() - Set or return the text content of the selected element

  • html() - Set or return the contents of the selected element (including HTML tags)

  • val() - Set or return the value of the form field

The following example shows how to set up content with text(), html(), and val():

$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});

Try it out . . .



the callback functions for text(), html(), and val().

The three jQuery methods above: text(), html() and val(), also have callback functions. T he callback function has two parameters: the subsector of the current element in the list of selected elements, and the original (old) value. Then return the string you want to use with the new value of the function.

The following example demonstrates text() and html() with callback functions:

$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "Old text: " + origText + " New text: Hello world!
(index: " + i + ")";
});
});

$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "Old html: " + origText + " New html: Hello <b>world!</b>
(index: " + i + ")";
});
});

Try it out . . .



Set Properties - attr()

The jQuery attr() method is also used to set/change property values.

The following example shows how to change the value of the href property in the (settings) link:

$("button").click(function(){
$("#w3s").attr("href","//www.w3cschool.cn/jquery");
});

Try it out . . .

The attr() method also allows you to set multiple properties at the same time.

The following example shows how to set both the href and title properties:

$("button").click(function(){
$("#w3s").attr({
"href" : "//www.w3cschool.cn/jquery",
"Title": "jQuery tutorial"
});
});

Try it out . . .

Tip: Please refer to this site for more information about the jQuery attr() method!


The callback function of attr().

The jQuery method attr(), which also provides a callback function. T he callback function consists of two parameters: the subsector of the current element in the list of selected elements, and the original (old) value. Then return the string you want to use with the new value of the function.

The following example demonstrates an attr() method with a callback function:

$("button").click(function(){
$(" #w3cschool ").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});

Try it out . . .

Related articles

For jQuery, the ability to operate the DOM is important. jQuery provides a range of DOM-related methods that make it easy to access and manipulate elements and properties: refer to the "jQuery - DOM Operations" section of this site for details.