Jquery Set
jQuery - Set Content in addition to Attributes
Set Content - text(), html(), in addition to val()
We volition job the same iii methods from the previous page to set content:- 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 shape fields
Example
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
Influenza A virus subtype H5N1 Callback Function for text(), html(), in addition to val()
All of the iii jQuery methods above: text(), html(), in addition to val(), likewise come upwards alongside a callback function. The callback role has 2 parameters: the index of the electrical flow chemical cistron inward the listing of elements selected in addition to the master copy (old) value. You in addition to hence provide the string y'all wishing to job equally the novel value from the function.The next event demonstrates text() in addition to html() alongside a callback function:
Example
$("#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 + ")";
});
});
Set Attributes - attr()
The jQuery attr() method is likewise used to set/change attribute values.The next event demonstrates how to alter (set) the value of the href attribute inward a link:
Example
$("button").click(function(){
$("#w3s").attr("href", "https://www.w3schools.com/jquery");
});
The next event demonstrates how to laid upwards both the href in addition to championship attributes at the same time:
Example
$("button").click(function(){
$("#w3s").attr({
"href" : "https://www.w3schools.com/jquery",
"title" : "W3Schools jQuery Tutorial"
});
});
Influenza A virus subtype H5N1 Callback Function for attr()
The jQuery method attr(), likewise come upwards alongside a callback function. The callback role has 2 parameters: the index of the electrical flow chemical cistron inward the listing of elements selected in addition to the master copy (old) attribute value. You in addition to hence provide the string y'all wishing to job equally the novel attribute value from the function.The next event demonstrates attr() alongside a callback function:
Example
$("button").click(function(){
$("#w3s").attr("href", function(i, origValue){
return origValue + "/jquery";
});
});
Comments
Post a Comment