Lesson 13: jQuery Get Content
Master jQuery methods for retrieving content from HTML elements. Learn how to get text, HTML, values, and attributes with interactive examples.
Understanding jQuery Content Methods
jQuery provides powerful methods to retrieve content from HTML elements. These methods allow you to get text content, HTML structure, form values, and element attributes.
text()
- Gets text content of elementshtml()
- Gets HTML content including tagsval()
- Gets the value of form fieldsattr()
- Gets the value of an attributeprop()
- Gets the property value of an element
Method | Description | Example |
---|---|---|
text() |
Returns text content without HTML tags | $("p").text() |
html() |
Returns content including HTML tags | $("div").html() |
val() |
Returns value of form elements | $("input").val() |
attr() |
Returns value of specified attribute | $("a").attr("href") |
text() Method
The text()
method retrieves the combined text content of the selected elements and their descendants.
Example 13.1: Getting Text Content
Retrieve text content from elements:
// text() method example $("#text-btn").on('click', function() { const textContent = $("#text-demo").text(); $("#text-result").text(textContent); }); $("#text-reset").on('click', function() { $("#text-result").text("Text content will appear here"); });
html() Method
The html()
method retrieves the HTML content (inner HTML) of the first matched element.
Example 13.2: Getting HTML Content
Retrieve HTML content from elements:
// html() method example $("#html-btn").on('click', function() { const htmlContent = $("#html-demo").html(); $("#html-result").text(htmlContent); }); $("#html-reset").on('click', function() { $("#html-result").text("HTML content will appear here"); });
val() Method
The val()
method retrieves the current value of the first matched form element.
Example 13.3: Getting Form Values
Retrieve values from different form elements:
// val() method example $("#val-btn").on('click', function() { const name = $("#name").val(); const email = $("#email").val(); const country = $("#country").val(); const result = `Name: ${name}, Email: ${email}, Country: ${country}`; $("#val-result").text(result); }); $("#val-reset").on('click', function() { $("#val-result").text("Form values will appear here"); });
attr() Method
The attr()
method retrieves the value of the specified attribute from the first matched element.
Example 13.4: Getting Attribute Values
Retrieve various attribute values:
// attr() method example $("#attr-btn").on('click', function() { const imgSrc = $("#attr-img").attr("src"); const linkHref = $("#attr-link").attr("href"); const dataInfo = $("#attr-div").attr("data-info"); const result = `Image Source: ${imgSrc}\nLink Href: ${linkHref}\nData Info: ${dataInfo}`; $("#attr-result").text(result); }); $("#attr-reset").on('click', function() { $("#attr-result").text("Attribute values will appear here"); });
Combining Methods
Combine different getter methods to retrieve complex information from elements.
Example 13.5: Combined Content Retrieval
Get multiple content types from elements:
Card 1
First content card
With multiple lines
Card 2
Second content card
Card 3
Third content card
// Combined method example $("#combined-btn").on('click', function() { const card1Text = $("#card1").text(); const card2Html = $("#card2").html(); const card3Data = $("#card3").attr("id"); const result = `Card1 Text: ${card1Text}\nCard2 HTML: ${card2Html}\nCard3 ID: ${card3Data}`; $("#combined-result").text(result); }); $("#combined-reset").on('click', function() { $("#combined-result").text("Combined content will appear here"); });
Practical Applications
jQuery content retrieval methods are versatile and can be used in many real-world scenarios:
Form Validation
Get form values to validate user input before submission.
Dynamic Content
Retrieve content to update other parts of the page dynamically.
Data Extraction
Extract data attributes for JavaScript processing.
URL Handling
Get href attributes to manage navigation and link behavior.
Interactive Content Playground
Click any card to see content retrieval in action:
Text Demo
Click to get text content
HTML Demo
Click to get HTML content
Attribute Demo
Click to get data attribute
// Interactive playground $("#play1").on('click', function() { const content = $(this).text(); $("#play-result").text(`Text Content: ${content}`); }); $("#play2").on('click', function() { const content = $(this).html(); $("#play-result").text(`HTML Content: ${content}`); }); $("#play3").on('click', function() { const content = $(this).attr("data-info"); $("#play-result").text(`Data Attribute: ${content}`); });
Best Practices
Follow these guidelines when retrieving content with jQuery:
- Check element existence before getting content
- Use data attributes for custom element data
- Sanitize HTML content when working with user-generated content
- Cache selectors when accessing the same element multiple times
- Use prop() instead of attr() for boolean properties like 'checked'
// Good practices for content retrieval // 1. Check element existence if ($("#element").length) { const content = $("#element").text(); } // 2. Using data attributes <div id="user" data-id="123" data-role="admin"></div> const userId = $("#user").data("id"); // 3. Sanitizing HTML content const rawHtml = $("#userContent").html(); const cleanHtml = sanitizeHTML(rawHtml); // 4. Caching selectors const $userForm = $("#userForm"); const name = $userForm.find("#name").val(); const email = $userForm.find("#email").val(); // 5. Using prop() for boolean properties const isChecked = $("#checkbox").prop("checked");