Three simple, but useful, jQuery methods for DOM manipulation are:
$("#btn1").click(function(){ alert("Text: " + $("#test").text()); }); $("#btn2").click(function(){ alert("HTML: " + $("#test").html()); }); $("#btn1").click(function(){ alert("Value: " + $("#test").val()); }); $("button").click(function(){ alert($("#w3s").attr("href")); }); $("button").click(function(){ alert($("#w3s").addAttr("href","www.yahoo.com")); }); $("button").click(function(){ alert($("#w3s").removeAttr("href")); });
With jQuery, it is easy to add new elements/content.
We will look at four jQuery methods that are used to add new content:
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."; $("body").append(txt1, txt2, txt3); // Append the new elements } $("img").after("Some text after"); $("img").before("Some text before");
jQuery, it is easy to remove existing HTML elements.
To remove elements and content, there are mainly two jQuery methods:
$("#div1").remove(); $("#div1").empty();
With jQuery, it is easy to manipulate the style of elements.
jQuery has several methods for CSS manipulation. We will look at the following methods:
$("button").click(function(){ $("h1, h2, p").addClass("blue"); $("div").addClass("important blue"); }); $("button").click(function(){ $("h1, h2, p").removeClass("blue"); }); $("button").click(function(){ $("h1, h2, p").toggleClass("blue"); }); $("p").css("background-color"); $("p").css("background-color", "yellow");
With jQuery, it is easy to work with the dimensions of elements and browser window.
jQuery has several important methods for working with dimensions:
$("button").click(function(){ var txt = ""; txt += "Width: " + $("#div1").width() + ""; txt += "Height: " + $("#div1").height(); $("#div1").html(txt); }); $("button").click(function(){ var txt = ""; txt += "Inner width: " + $("#div1").innerWidth() + ""; txt += "Inner height: " + $("#div1").innerHeight(); $("#div1").html(txt); }); $("button").click(function(){ var txt = ""; txt += "Outer width: " + $("#div1").outerWidth() + ""; txt += "Outer height: " + $("#div1").outerHeight(); $("#div1").html(txt); });
Total : 27273
Today :9
Today Visit Country :