jQuery Methods to Get and Set Content

jQuery provides several methods to get and set content in HTML elements. Here are the main methods:

1. text()

Get content: Returns the combined text content of selected elements (excluding HTML tags)

Set content: Sets the text content of selected elements (HTML tags are escaped)

2. html()

Get content: Returns the HTML content of the first matched element (including HTML tags)

Set content: Sets the HTML content of selected elements (HTML tags are rendered)

3. val()

Get value: Returns the value of form elements (input, select, textarea)

Set value: Sets the value of form elements

4. attr()

Get attribute: Returns the value of a specified attribute

Set attribute: Sets the attribute value

5. prop()

Get property: Returns the value of a property (for boolean attributes like checked, disabled)

Set property: Sets the property value

6. data()

Get data attribute: Returns the value of a data attribute

Set data attribute: Sets the data attribute value

7. append() / prepend()

Add content at the end or beginning of selected elements

8. after() / before()

Add content after or before selected elements

9. replaceWith()

Replace selected elements with new content

10. empty() / remove()

empty(): Removes all child nodes and content from selected elements

remove(): Removes the selected elements themselves

These methods provide flexible ways to manipulate content in the DOM using jQuery. Choose the appropriate method based on whether you need to work with text, HTML, form values, or attributes.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.