Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

assign javascript var to div style

Posted on 2013-01-31
8
Medium Priority
?
375 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
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 44

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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

722 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