Is it possible to change custom DOM changes at runtime?

Possible duplicate:
javascript object, access variable property name?

I was messing around with JS while studying stuff, and I'm curious about something ... Let's say you have the aFunc() function and you accept the aFunc(val) . The value is user defined and then used to change the CSS element.

Example:

 function aFunc(val){ document.getElementById('something').style.val = 'red'; } 

Say the user has entered borderColor , he will somehow fail borderColor , where val . I do not know how and if it is possible.

EDIT: Please don't eval() :)

+4
source share
2 answers

Just use it as a base: JSBIN-Demo on Div

 var type = prompt("style"); var value = prompt("value"); document.body.style[type] = value; 
+4
source

Each object in JavaScript has a hasOwnProperty method, which takes a string value and returns a boolean value.

 var myObj = { name: "Josh" }; myObj.hasOwnProperty("name") === true; //This is true 

You can use this to check for the presence of a specific property, and then use the method specified in the Akhil Sekharan answer to access this property.

+2
source

All Articles