1

I am trying to add css property to my css variable in Jquery.

var cssProp = {
    'position': 'absolute',
    'top': topPos,
    'left': leftPos
}

//this codes works
cssProp.top += 10;

//this is not..
cssProp.z - index = 10;

How to fix this? Thanks a lot!

1
  • 2
    Have you tried cssProp.zIndex=10;? Commented Sep 7, 2012 at 20:22

5 Answers 5

2

Dashes in properties when using dot notation are not allowed

cssProp['z-index'] = 10;

Note that css properties when talking about element.style all dashed elements in CSS are camel cased in JavaScript so it would propbably actually be cssProp.zIndex = 10;

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

Comments

0

Possibly because z-index is not defined in cssProp

Try

var cssProp = {'position':'absolute',
                 'top': topPos,
                 'left': leftPos,
                 'z-index': 0
            }

Comments

0

Doing it the way you are you are creating a new variable called "z-index", which is invalid in javascript, and therefore not working.

Try calling it "zIndex" instead, that is a valid javascript variable name.

Comments

0

Using - (dash) as a variable name/object key is not valid in javascript. Try using camelCase notation as zIndex.

Read more:

Are dashes allowed in javascript variable names?

Comments

0

Like this

var cssProp = {
    'position':'absolute',
    'top': topPos,
    'left': leftPos,
    'zIndex': '0'
};

jQuery always removes the '-' and then concatenates the word using the camelcase style type. I believe jQuery then has a regular expression that accomplishes this...

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.