-1

i'm looking for a way to set a variable as a property (for an element same as a div)
here is the code:

function set_it(element,property){
    return element.style.property;
}
console.log(set_it(document.getElementById("div1"),"left"));

at last line i'm trying to get "left" property of "div1" by the function
but I get undefined in console
I know , actually it will look for a property named "property" , which doesn't exist
but how can I do this ?
thanks

mk rowling
  • 203
  • 1
  • 12

1 Answers1

1
function set_it(element,property){
    return element.style[property];
}

Like so! You can use variables as object accessors when you use square brackets, not dot notation.

benhowdle89
  • 36,900
  • 69
  • 202
  • 331