0

I found this question on editing css styles in Javascript, in which the solution is to use

document.getElementById('element').style.MozBoxShadow = "1px 1px 1px #000";

I am trying to write a function that takes an array of parameters to edit as so:

var element = document.getElementById('element');
editElementStyles(element, new Array("width", "100%", "height", "100%"...etc.));

And the function:

function editElementStyles(element, args) {
    for (var i = 0; i < args.length; i += 2) {
        element.style.args[i] = args[i + 1];
    }   
}

However, this of course does not work as args[i] is not a property of element.style. How can I make this work?

2
  • I am open to JQuery, but would prefer not to go there. Commented Nov 18, 2014 at 23:23
  • 1
    FYI, new Array(elem1, elem2, elem3, ...) can be expressed more simply as [elem1, elem2, elem3, ...]. Commented Nov 18, 2014 at 23:26

5 Answers 5

2

Simply:

element.style[args[i]] = args[i + 1];

You correctly note that args[i], and args, is not a property of element.style, to counter this you simply use square-bracket notation, rather than dot-notation, to access the properties. Which, incidentally, you were doing with args[i] anyway.

This results in:

function editElementStyles(element, args) {
    for (var i = 0; i < args.length; i += 2) {
        element.style[args[i]] = args[i + 1];
    }   
}

Incidentally, this seems to be a risky way of associating properties and values; I'd suggest:

function editElementStyles (element, styles) {
    for (var prop in styles) {
        if (styles.hasOwnProperty(prop)) {
            element.style[prop] = styles[prop];
        }
    }
}

editElementStyles (elem, {'width' : '100%', 'color' : 'red'});
Sign up to request clarification or add additional context in comments.

Comments

2

You may want to change your function to accept an object since what you are really looking for is a key/value pair

editElementStyles(element, {"width" : "100%", "height" : "100%"});


function editElementStyles(element, args) {
    for (var key in args) {
        element.style[key] = args[key]
    }   
}

Comments

1

Just use the square bracket notation instead, that allows you to use the value of variables for the property name to look up.

i.e. change your function to:

function editElementStyles(element, args) {
    for (var i = 0; i < args.length; i += 2) {
        element.style[args[i]] = args[i + 1];
    }   
}

Comments

1

Instead of the point notation you can always access such properties in the following ways:

man.age=25

is equivalent to

man['age']=25

So, try this

element.style[args[i]]= args[i + 1];

Comments

1

Try using:

element.style[args[i]] = args[i+1];

You can access properties through object[property] as you do with object.property.

However as other answers suggests it's better to use a json object instead of array to pass the properties names and values.

1 Comment

That last . isn't required.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.