How to change css from javascripts

Tags: javascript,css,jquery-ui

Problem :

How can I change CSS from javascripts. Im using JQUERY (Dialog) and I want to change the style of a DIV from javascript.


Solution :

Check out the jQuery documentation. If you want anything it will be there.

Anyhow, if you want to add styles to elements, you need to use the css function, which has a few variants.

$(selector).css(properties);  // option 1
$(selector).css(name, value); // option 2

So if you have a DIV with ID of "mydiv" and you want to make the background red, you would do

$("div#mydiv").css({'background-color' : 'red'}); // option 1
$("div#mydiv").css('background-color','red');     // option 2

The first way is easier if you're setting multiple things at once.

If you want to check what a property is currently set to, you would use a variant of the 2nd option, just omit the value.

var color = $("div#mydiv").css('background-color');

Would make the var color be red if you already set it above, for example.

You can also add and remove classes, doing something like


    CSS Howto..

    how do I use standard GWT CSS style themes from non-GWT pages?

    How to resize div with CSS by the border

    Can't figure out how to make a box with multiple images with CSS/HTML

    How to add a border around a canvas?

    How can I make this menu using only ul, li and css?

    How to insert css arrow into html page and make it clickable

    How to place output from an external php file to a specific place on the page

    How do you set the scroll bar skin on a single List instance?

    How to wrap text using CSS? [duplicate]

    How to set one CSS rule for two tags?

    How to apply CSS correctly to iFrame?

    How to use jQuery without making a mess? [closed]

    How do I use PHP to apply css properties? [closed]

    How to add space between table cells `td`?

    How to make a round mask over a image

    how to target individual elements in grails g:render

    How can I give a javascript element a CSS class?

    CSS - how to code the left column gets narrower when resizing the window?

    How to Write CSS Code for Mozilla Firefox Browser

    How to show a small image to the right of a div with CSS?

    How to fix layout to browser window without scroll

    How do I change the Mailchimp subscribe button color using inline CSS?

    How to stop text in div pushing other divs down the page?

    How to write this 'OR' CSS selector?

    How to implement CSS file style on selected div

    how to correctly render a response on an gsp from on the same page as the request (REST call) in grails

    How can that view be achieved? Collage / Magazine Products

    How to use environment variables in ReactJS imported CSS?

    How to make UL Tabs with only HTML CSS

    how to select margin for a background image in CSS/CSS3?