Solved

Add a Style Attribute to Div using Javascript function

Posted on 2009-05-19
4
7,907 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
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
RQuadling earned 250 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 250 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:RQuadling
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Animate a Title 7 35
Menu Inconsistent 3 20
Flexbox in CSS3 2 25
(URGENT) Help HTML and CSS Resizing Images 2 20
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now