Solved

assign javascript var to div style

Posted on 2013-01-31
8
362 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
  • 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
Technology Partners: 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

Technology Partners: 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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 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