2

I want to add css with JQuery. I've try this :

$('#imageMenu').css("display", "block");

but the generate code is inline. Can I just add css without made inline style? I want to add style in my stylesheet. It's possible? I search on the internet but I not found.

Thanks

2
  • Take a look here Add Rules to Stylesheets with JavaScript Commented Nov 29, 2014 at 22:44
  • 1
    Use something like this: sheet.insertRule("header { float: left; opacity: 0.8; }", 1); Commented Nov 29, 2014 at 22:45

2 Answers 2

5

what is the reason you are trying to do it? if you know the css code you need from the first place you can add it to your stylesheet file and then use the JQuery addClass function:

CSS (insert it to your stylesheet file):

.block
{
    display: block;
}

JQuery (make the action):

$('#imageMenu').addClass('block');

example: http://jsfiddle.net/2dazvud6/

improve of Toby: Keep in mind, anything done with JavaScript will be at least partially inline. In this example the class you add is added inline. This is simply because of how JavaScript works. It is client sided and the client does not have access to your website to make edits. If you need a strong level of control you may have to consider using JavaScript/Ajax and a sever sided scripting language instead.

Sign up to request clarification or add additional context in comments.

Comments

0

You can add rules to the stylesheet using Javascript: For example:

myStyle.insertRule('#blanc { color: white }', 0);

Note that this won't make changes to the stylesheet file itself, just the stylesheet that is loaded in memory. More info can be found here: CSSStyleSheet.insertRule()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.