text() - Sets or returns the text content of selected elements
html() - Sets or returns the content of selected elements (including HTML markup)
val() - Sets or returns the value of form fields
syntax
$(selector).text("Any String");
$(selector).html("Any String");
$(selector).val("Any String");
This is a paragraph.
This is another paragraph.
Input field:
Sets the html content using a callback
All of the three jQuery methods above: text(), html(), and val(), also come with a callback function. The callback function has two parameters: the index of the current element in the list of elements selected and the original (old) value. You then return the string you wish to use as the new value from the function.
syntax
$(selector).text(function (current element index, current value));
return "new string";
});
This is a bold paragraph.
This is another bold paragraph.
The following example demonstrates how to change (set) the value of the href attribute in a link:
Mouse over the link (or click on it) to see that the value of the href attribute has changed.
The jQuery method attr(), also come with a callback function. The callback function has two parameters: the index of the current element in the list of elements selected and the original (old) attribute value. You then return the string you wish to use as the new attribute value from the function.
syntax
$(selector).click(function(){
$(selector).attr({
"href", function(i, origValue) {
return new value
});
});
Mouse over the link (or click on it) to see that the value of the href attribute has changed.
The jQuery append() method inserts content AT THE END of the selected HTML elements.
syntax
$(selector).append("some appendend text");
This is a paragraph.
This is another paragraph.
List item 1
List item 2
List item 3
The jQuery prepend() method inserts content AT THE BEGINNING of the selected HTML elements.
syntax
$(selector).prepend("some appendend text");
This is a paragraph.
This is another paragraph.
List item 1
List item 2
List item 3
Both the append() and prepend() methods can take an infinite number of new elements as parameters. The new elements can be generated with text/HTML (like we have done in the examples above), with jQuery, or with JavaScript code and DOM elements..
syntax
function appendText()
{
var txt1="< p >Text.< /p >"; // Create element with HTML
var txt2=$("< p >< /p >").text("Text."); // Create with jQuery
var txt3=document.createElement("p"); // Create with DOM
txt3.innerHTML="Text.";
$("p").append(txt1,txt2,txt3); // Append the new elements
}
This is a paragraph.
after() method inserts content AFTER the selected HTML elements.
before() method inserts content BEFORE the selected HTML elements.
syntax
$(selector).after("Some text after");
$(selector).before("Some text before");
Main line
The jQuery remove() method removes the selected element(s) and its child elements.
syntax
$(selector).remove()
This is some text in the div.
This is a paragraph in the div.
This is another paragraph in the div.
empty() method removes the child elements of the selected element(s).
syntax
$(selector).empty()
This is some text in the div.
This is a paragraph in the div.
This is another paragraph in the div.
remove() method also accepts one parameter, which allows you to filter the elements to be removed
syntax
$(selector).remove(".italic")
This is a paragraph in the div.
This is another paragraph in the div.
This is another paragraph in the div.
Add class attributes to different elements. Of course you can select multiple elements, when adding classes: