Solved

assign javascript var to div style

Posted on 2013-01-31
8
367 Views
Last Modified: 2013-01-31
I have a javascript string variable for background color.
<script>  var  bg_color="black"; </script>
How do I assign this var to div background-color?
<div class="wrapper" style="background-color:??????">
0
Comment
Question by:yaron89
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 30

Expert Comment

by:LZ1
ID: 38839838
Just so you know, it's a very bad practice to have inline styles on your page. Why aren't you setting this on the class or ID of the element instead?
0
 

Author Comment

by:yaron89
ID: 38839848
I have to change it dynamically
0
 
LVL 30

Expert Comment

by:LZ1
ID: 38839873
I would use jQuery, because for me it's easier.
There's 2 ways of doing it. 1 would be to set it inline like you have above. The 2nd and the one I would use would be to add a class that overrides the default styling.

$(document).ready(function(){
     $(".wrapper").click(function(){
	//$(this).css("background", "#000000")
           $(this).addClass("black");
	});
});

Open in new window


You can replace the click function with whatever you need.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 38839905
Without jQuery, something like this:

<script type="text/javascript">
var bg_color = '#ff0000';
			
function setBg(element) {
	element.style.backgroundColor = bg_color;
}
</script>

<div class="wrapper" onclick="setBg(this)">This is a test</div>

Open in new window

0
 

Author Comment

by:yaron89
ID: 38839948
It works with onclick event but not with onload. I need to change the background when the page is loading.
0
 
LVL 30

Expert Comment

by:LZ1
ID: 38839958
Then just this:
			$(document).ready(function(){
				$('.wrapper').addClass("black");
				});

Open in new window

0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 38839963
Give the relevant DIV an ID (so you can select it when loading) and then use the following:

<script type="text/javascript">
var bg_color = '#ff0000';
			
function setBg(element) {
	x = document.getElementById(element);
	x.style.backgroundColor = bg_color;
}
<script>

<body onLoad="setBg('myDiv')">
	<div id="myDiv" class="wrapper">This is a test</div>
</body>

Open in new window

0
 

Author Closing Comment

by:yaron89
ID: 38840009
Thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question