2

I've got the following code

var addressPopupMenu = window.createPopup();
function showAddressPopup() {
    var popup = document.getElementById('addressFullSpan');

    popupMenuBody = popupMenu.document.body;
    popupMenuBody.style.backgroundColor = "#336699";
    popupMenuBody.style.border = "solid 2px; white";
    popupMenuBody.style.fontSize="130%";
    popupMenuBody.style.color="white";
    popupMenuBody.style.padding="10px";
    popupMenuBody.style.paddingLeft="30px";

As you can see I'm repeating popupMenuBody.style. How can I give popupMenuBody.style a css class so I dont have to repeat this for every popup

edit: it's not working

I've added popupMenuBody.className = "popups";

.popups
    {
        background-color: #29527A;
        border: solid 2px; white;
        fontSize:120%;
        pcolor:white;
        padding:10px;
        paddingLeft:30px;
        textTransform:capitalize;
    }

also yes, i am including the .css in my page its working else where on the page

1
  • Amazing, three suggestions for JQuery and all the OP wants to do is style a single element. Commented Mar 8, 2011 at 9:48

8 Answers 8

5

popupMenuBody.className = "class_name";

Sign up to request clarification or add additional context in comments.

4 Comments

it doesn't work for some reason. theres no styling to the popup now
Do you have a CSS rule defined that contains the styles you're after?
I notice you're opening a popup menu... does that page reference your CSS styles?
Also, your CSS is invalid - that may have something to do with it.
1
popupMenuBody.className = "my class";

.popups
    {
        background-color: #29527A;
        border: solid 2px white;
        font-size: 120%;
        color: white;
        padding: 10px;
        padding-left: 30px;
        text-transform: capitalize;
    }

1 Comment

CSS definitions are NOT the same as IE dom properties. Your CSS is invalid. I've included valid CSS.
0

use jquery addClass? http://api.jquery.com/addClass/

2 Comments

Why the overhead of using jQuery when it be done rather easily with plain js ?
it's just an alternative.. not a must
0

I would do it using Jquery and addClass():

$('#addressFullSpan').addClass('name')

Comments

0

jQuery has the method addClass() for applying a CSS class to an element

http://api.jquery.com/addClass/

Comments

0

Have you tried :

var addressPopupMenu = window.createPopup();
function showAddressPopup() {
    var popup = document.getElementById('addressFullSpan');
    popupMenuBody = popupMenu.document.body;
    popupMenuBody.className = "className";
    ...
}

Comments

0

To assign a CSS class to an element created in JavaScript, you can just use the following line of code:

popupMenuBody.className = "popups";

You've said that this doesn't work, but this is actually because your CSS is broken. I'm assuming that you've copy/pasted some JavaScript into your CSS file and changed it a bit, and as a result, your property names are all wrong. What you actually need for your CSS is this:

.popups
{
    background-color: #29527A;
    border: solid 2px white;
    font-size:120%;
    color:white;
    padding:10px;
    padding-left:30px;
    text-transform:capitalize;
}

Notice that I have:

  1. removed the ";" between "2px" and "white"
  2. renamed "fontSize" to "font-size"
  3. renamed "pcolor" to "color"
  4. renamed "paddingLeft" to "padding-left"

Your CSS should now get applied correctly.

Comments

0

To avoid overwriting existing classnames on popupMenuBody, do this:

popupMenuBody.className += ' class_name';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.