9951 explained code solutions for 126 technologies


jqueryHow do I add styles to an element using jQuery?


Adding styles to an element using jQuery is easy and straightforward. You can use the css() method to set the style of the element. Here's an example code block:

$('#example').css({background-color: 'red', color: 'blue'});

This code will set the background-color of the element with the id of "example" to red, and the color of the text to blue.

The css() method takes an object as an argument. The object should have a list of the css properties you want to set and their respective values. Here's a list of the parts of the code above:

  • $('#example') - This is a jQuery selector that selects the element with the id of "example".
  • css() - This is the method used to set the style of the element.
  • {background-color: 'red', color: 'blue'} - This is the object that contains the list of css properties and their values.

Here are some relevant links for more information:

Edit this code on GitHub