Home >Web Front-end >JS Tutorial >Example of jquery setting text value (setting text box DIV form value)_jquery
jquery setting content - text(), html() and val()
We will use the same three methods from the previous chapter to set the content:
text() - Sets or returns the text content of the selected element
html() - Sets or returns the content of the selected element (including HTML tags)
val() - Sets or returns the value of a form field
The following example demonstrates how to set content through the text(), html() and val() methods:
Example
Callback functions of text(), html() and val()
The three jQuery methods above: text(), html() and val() also have callback functions. The callback function takes two parameters: the index of the current element in the selected element list, and the original (old) value. Then return the string you wish to use as the function's new value.
The following example demonstrates text() and html() with callback functions:
Example
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "Old html: " origText " New html : Hello world!
(index: " i ")";
});
});