Solved

assign javascript var to div style

Posted on 2013-01-31
8
355 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
Comment Utility
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
Comment Utility
I have to change it dynamically
0
 
LVL 30

Expert Comment

by:LZ1
Comment Utility
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
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:yaron89
Comment Utility
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
Comment Utility
Then just this:
			$(document).ready(function(){
				$('.wrapper').addClass("black");
				});

Open in new window

0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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
Comment Utility
Thank you
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now