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> Link to comment https://forums.phpfreaks.com/topic/287514-jquery-changes-css-property/ 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); Link to comment https://forums.phpfreaks.com/topic/287514-jquery-changes-css-property/#findComment-1474989 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.