Skip to main content

jQuery Set Methods

jQuery - Set Methods

Set methods in jQuery are used to modify the properties or attributes of HTML elements. These methods allow you to change various aspects of elements such as their CSS styles, content, attributes, and more.


1. .css()

The .css() method is used to set one or more CSS properties for the selected elements.

Example:

// Set CSS properties
$("#myElement").css({
    "color": "red",
    "font-size": "20px",
    "background-color": "#eee"
});

2. .html()

The .html() method is used to set the HTML content of the selected elements.

Example:

// Set HTML content
$("#myElement").html("Hello, World!");

3. .text()

The .text() method is used to set the text content of the selected elements.

Example:

// Set text content
$("#myElement").text("Hello, World!");

4. .attr()

The .attr() method is used to set attributes for the selected elements.

Example:

// Set attributes
$("#myElement").attr("src", "image.jpg");

5. .addClass()

The .addClass() method is used to add one or more classes to the selected elements.

Example:

// Add class
$("#myElement").addClass("highlight");

6. .removeClass()

The .removeClass() method is used to remove one or more classes from the selected elements.

Example:

// Remove class
$("#myElement").removeClass("highlight");

7. .toggleClass()

The .toggleClass() method is used to toggle between adding and removing one or more classes from the selected elements.

Example:

// Toggle class
$("#myElement").toggleClass("highlight");

8. .prop()

The .prop() method is used to set properties for the selected elements.

Example:

// Set properties
$("#myElement").prop("disabled", true);

9. Conclusion

These are some of the most commonly used set methods in jQuery, allowing you to dynamically modify the properties and attributes of HTML elements in your web pages.

Comments