0

I have a situation in my project , in css I have a class

which is empty for now

#version_mobile.hidden
{

}

and in js Im doing this

this.$("#version_mobile.hidden").css({right: - window.text_mobile_width});

(I supose my selector is bad ?)

how to add "right" atribute to this class with this dynamically created value ?

to class become

#version_mobile.hidden
    {
     right : -450px;
    }

Btw I need to use this class because the animation is working with it :/

1
  • Sry my bad in css is #version_mobile.hidden not .version_mobile.hidden Commented Sep 12, 2013 at 11:27

5 Answers 5

2

.css() function changes the inline css style but has no effect over the css classes at all.

As pointed out in the documentation:

When using .css() as a setter, jQuery modifies the element's style property.

You can also change the classes by using the addClass(), removeClass() or even the toggleClass() functions of jQuery.

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

Comments

0

You cannot add to the class properties, but you can apply rules to the element style.

2 Comments

I dont need this , I need to that element class .hidden gets right:-450px
Ok, so instead of changing the class itslef, change all the elements that has it. $('.yourClass').css('right', '-450px')
0
this.$("#version_mobile.hidden").css({"right", "- window.text_mobile_width"});

you can not add definations for class in jquery. but you can add any style to your selecter.

1 Comment

Sry my bad in css is #version_mobile.hidden not .version_mobile.hidden
0

What are you trying to achieve?

you cannot add a property to the css file using this.

what you should look at is you apply this id or class to your html elements

and access the elements in the javascript using the jquery selectors

$(#selector) and modify the property using .css.

So you will achieve the same result this way as any existing property style for that element will be overridden with your latest style put through the jquery.

Comments

0

It's technically possible to modify style rules on the fly, but it's difficult.

In the document object you will find an (array) property called styleSheets, with one entry for each referenced stylesheet.

Each stylesheet object (of type CSSStyleSheet) has an insertRule method, which you can use to create new rules, or delete existing rules. What appears to be difficult is enumerating the existing set of rules so you can find which one to modify or delete.

That said, it's generally far preferred to simply change an element's classes than to try to dynamically change the styling of an existing class.

See also http://davidwalsh.name/add-rules-stylesheets

2 Comments

Yes but value od right position im getting from counting in js , how than change that in css dinamically ??
what I've written above should be sufficient to allow you to dynamically create a style. I wouldn't suggest modifying an existing one.

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.