jQuery — Get Content
Getting Text
var text = $("p").text();
console.log(text);
Getting HTML
var html = $("div").html();
console.log(html);
Getting Attributes
var href = $("a").attr("href");
var src = $("img").attr("src");
var id = $("div").attr("id");
Getting Multiple Attributes
var attrs = $("img").attr(["src", "alt", "title"]);
console.log(attrs.src, attrs.alt, attrs.title);
Getting Form Values
var inputVal = $("input").val();
var selectVal = $("select").val();
var textareaVal = $("textarea").val();
Getting CSS Values
var color = $("h1").css("color");
var width = $("div").css("width");
Getting Dimensions
var width = $("div").width();
var height = $("div").height();
var outerWidth = $("div").outerWidth();
var outerHeight = $("div").outerHeight();
Getting Position
var offset = $("div").offset();
console.log(offset.top, offset.left);
var position = $("div").position();
console.log(position.top, position.left);
Mini Practice
- Get text from a paragraph
- Get the href from a link
- Get a form input's value
- Get an element's width and height
Up Next
Continue with Set Content — setting content on elements.
Related Topics
Frequently Asked Questions about Get Content
What is Get Content in jQuery?
Get Content is a fundamental concept in jQuery. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Get Content?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Get Content.
Why is Get Content important in jQuery?
Get Content is essential for jQuery development. Understanding this concept will help you write better code and solve real-world problems more effectively.