Style manipulation

var newDiv = $("<div>").html("div content")
               .addClass("w-50")
               .css({'border': '1px solid red', 'color':'blue'});

$("p").removeClass("myClass noClass")
      .addClass("yourClass");
methods description
css(propName) Returns the css property value for the first element.
css(propNames)

Returns array the CSS property values for the first element.

The propNames parameter is an array of one or more CSS properties.

css(propName, val | func)

Sets the value of a CSS property for each element.

The val parameter is a string or number.

The func parameter is a function that returns a property value for the current element and has two arguments:

  • index - the index of the current element (you can use the this keyword to access it)
  • value - old value of property
css(objPropVals)

Sets the CSS properties for each element.

The objPropVals parameter is an object of property-value pairs like {'border': '1px solid red', 'color':'blue'}.

addClass(clsNames | func)

Adds classes to each element.

The clsNames can be string with space-separated classes or array of class names.

The func parameter is a function that returns a string with space-separated classes or array of classes and has two arguments:

  • index - the index of the current element (you can use the this keyword to access it)
  • currentClassName - existing classes
removeClass(clsNames | func)

Removes classes from each element.

The clsNames can be string with space-separated classes or array of class names.

The func parameter is a function that returns a string with space-separated classes or array of classes and has two arguments:

  • index - the index of the current element (you can use the this keyword to access it)
  • className - old classes
toggleClass(clsNames [, state])

Adds or removes classes from each element.

The clsNames can be string with space-separated classes or array of class names.

The state parameter is a boolean (not just truthy/falsy) value that determines whether the class should be added or removed.

toggleClass(func [,state])

Adds or removes classes from each element.

The state parameter is a boolean (not just truthy/falsy) value that determines whether the class should be added or removed.

The func parameter is a function that returns a string with space-separated classes or array of classes and has three arguments:

  • index - the index of the current element (you can use the this keyword to access it)
  • className - old classes
  • state