Tags
jQuery , css
Asked 2 years ago
25 Apr 2022
Views 670
ajamil

ajamil posted

jQuery css() Method

How to use the jQuery css method ?
i am using follwing way and it giving me error

$.css('color').val('red')


i am getting following error

Uncaught TypeError: Cannot read property 'replace' of undefined

ravi

ravi
answered Apr 25 '22 00:00


elementobject.css("propertyname");

by css() method either you can set the value or get the value of the property
for suppose:
1. css() setter method
if you want to set the color to black to specific element can be done as like below code :

$("span").css("color","black")

so above code will set the color to black for all span tag to current html page.

2. css() getter method
if you want to get the color from a specific element can be done as like below code :

var spancolor=$(".spanclass").css("color")

so above code will get the color property value for given class element


Nilesh

Nilesh
answered Apr 25 '22 00:00

one can set the multiple property as like below code :


$("div").css("color": "green", "font-weight": "bold")


so above code will set all div text to color as green, font-weight property to bold
so syntax for CSS method with multiple parameters.

elementobject.css({"propertyname":"value","propertyname":"value",......});


Post Answer