arunpatal Posted April 4, 2014 Share Posted April 4, 2014 Hi, The code below work fine when i want to change the value of background-color property <script> $(document).ready(function(){ $(".img_div").click(function(){ var element_value = $(this).val(); $("#main_div").css({"background-color":element_value}); }); }); </script> But when i want to change property itself (background-color), its not working <script> $(document).ready(function(){ $(".img_div").click(function(){ var element_prop = $(this).val(); $("#main_div").css({element_prop:"black"}); }); }); </script> Quote Link to comment Share on other sites More sharing options...
nogray Posted April 4, 2014 Share Posted April 4, 2014 You can't use a variable as an object key directly in JavaScript. You need to use brackets [] instead e.g. var obj = {}; var element_prop = $(this).val(); obj[element_prop] = 'black'; $("#main_div").css(obj); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.