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

x
?
Solved

Add a Style Attribute to Div using Javascript function

Posted on 2009-05-19
4
Medium Priority
?
7,923 Views
Last Modified: 2012-05-07
Hi All,

I have a DIV  container and I would like to be able to add ...style="background:#color"... through a javascript function of which the background colour is passed in as a argument..

function OpenDialog(strColour)
{
 Apply background style to MyDivId.
}

Many thanks,

Rit
0
Comment
Question by:rito1
[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
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 1000 total points
ID: 24426484
Are you using a JS library? I use Prototype (http://prototypejs.org).

Using prototype, I would do ...

$('MyDivId').style.backgroundColor=strColor;

NOTE: strColor needs to be look like #aabbcc where aa, bb, cc are the hex values for the RGB colours.


0
 
LVL 19

Assisted Solution

by:Albert Van Halen
Albert Van Halen earned 1000 total points
ID: 24426696
No need for libraries. Although it makes life much easier (my personal fav is jquery)
document.getElementById(myDivId).style.backgroundColor = strColor; // strColor being #rrggbb like RQuadling said (#ff0000 for red) or a named color like : red
 
via jQuery:
$("#" + myDivId).css("backgroundColor", strColor); 

Open in new window

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24426752
Albert is right about the libs. But I just find them so useful for consistency and provide some support for features lacking on older browsers.
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 31583232
Thanks both. Much appreciated.

Rit
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

715 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