Jquery set HTML attributes CSS classes and HTML form values and element content and data

Our Reader Score
[Total: 0 Average: 0]

# 2- Element Get and Set-

How to set HTML attributes: CSS styles and classes,  HTML form values, and element content and data

Some of the simplest and most common operations on jQuery objects are to get or set the value of HTML attributes, CSS styles, element content here we learn those methods.

#2.1 –  Get and Set HTML Attributies:

The attr() method is the jQuery getter/setter for HTML attributes,


The  attr() is jQuery’s master attribute-setting function, and you can use it to set things other than normal HTML attributes. If you use the attr() method to set an attribute named “css”, “val”, “html”, “text”, “data”, “width”, “height”, or “offset”, jQuery invokes the method that has the same name as that attribute and passes whatever value you specified as the argument.

For example, calling attr("css", {backgroundColor:"gray"}) is the same as callingcss({backgroundColor:"gray"}) .
We’ll learn about css(), val(), html(), and other methods in later article.

#2.2 –  Get and Set CSS Attributes:

The  css() method is very much like the attr() method, but it works only with the CSS styles of an element ie sets or returns one or more style properties for the selected elements.

Sample Code:


Get your Printable Copy