views:

138

answers:

6

Hey guys

I am coding a GM script, and one thing I realised that I'm doing repeatedly is doing the same code over and over again. Specifically, the style property.

function createButton() {
    var a = document.createElement('a');
    a.href = '#';
    a.innerHTML = 'Print Topic';
    a.style.position = 'absolute';
    a.style.right = '3em';
    a.style.top = '6em';
    a.style.fontFamily = 'Arial,Helvetica,sans-serif';
    a.style.fontWeight = 'bold';
    a.style.fontSize = '125%';
    a.style.background = '#777777 none repeat scroll 0 0';
    a.style.color = 'white';
    a.style.padding = '6px 12px';
    document.body.insertBefore(a, document.body.lastChild);
}

As you can see in my sample code, I repeatedly wrote a.style a lot of times. Do you have techniques that you use to avoid this mess? Just for the sake of gracefulness.

THANKS --

Guys, here's the reduced code:

function createButton() {
    var a = document.createElement('a');
    var css = document.createElement('style');
    css.type = 'text/css';
    css.innerHTML = '#prt { position:absolute; right:3em; top: 6em; font-family: Arial,Helvetica,sans-serif; font-weight:bold; font-size:125%; background: #777777 none repeat scroll 0 0; color: white; padding: 6px 12px;}'
    a.href = '#';
    a.innerHTML = 'Print Topic';
    a.id = 'prt';
    document.body.insertBefore(a, document.body.lastChild);
    document.body.appendChild(css);
}

LOL, that certainly looks better

+12  A: 

Put the style attributes into CSS classes, then just dynamically swap the classes instead of doing each style attribute explicitly.

tehblanx
<- this, dear jesus this
annakata
+1  A: 

The not-very-good-but-possibly-better-than-the-original answer:

s = a.style;

s.position = "absolute";
...etc...
s.color = "white";
somori
AFAIK, some browsers would treat s as a reference, and others as a copy so you might run into cross browser issues here. I could be totally wrong though.
Macha
if you were going to go that route, with(a.style) would be better unless you believe crockford's lies
annakata
Always in for a good lie. Tell me more!
KooiInc
Which browsers do a copy on assignment for JavaScript?What did Crockford say about this case?
somori
Thanks,I tried this before. Unfortunately it didn't work.
rymn
Just completely ignore my last comment. Someone should do one of these for stack overflow : http://xkcd.com/481/ .
Macha
+1  A: 

Try

If you were using jQuery you could write:

$("a").css({position: "absolute", right: "3em", top: "6em"}) // etc.
Macha
Thanks, I'm coding in Greasemonkey and want to know more about bare metal Javascript. =D
rymn
+1  A: 

jQuery makes things shorter via a magic function $() that gives back a wrapper of your dom element.

The wrapper gives you access to all the css property, and pretty much all its methods (ie the setters are giving back "this") including the CSS setters.

It will be clearer with an example...

$("<a href='toto/'></a>")
    .css("position", "absolute");
    .css("right", "3em")
    .appendTo($(containerid));
poulejapon
Yea, jQuery is great. I'm trying the bare metal Javascript now. I kind of want to know the very detail of Javascript.
rymn
+1  A: 

Javascript has a with statement...

with a.style {
  position = 'absolute';
  right = '3em';
}

And you can split your repeated functionality out as a function and pass in your element as a parameter...

function setStyle(elem) {
  with elem.style {
    position = 'absolute';
    right = '3em';
  }

  return elem
}

//Invoke like this: elem = setStyle(elem)
Josh Stodola
Does this work crossbrowser?
Macha
A: 

Elaborating on the most highly voted answer above. Just put all of your css information into a css class. Then just assigned the class attribute.

<style type='text/css'>
 a .prt { 
 position:absolute; 
 right:3em; 
 top: 6em; 
 font-family: Arial,Helvetica,sans-serif; 
 font-weight:bold; 
 font-size:125%;
 background: #777777 none repeat scroll 0 0;
 color: white; padding: 6px 12px;
}
</style>
<script>
function createButton() {    
 var a = document.createElement('a');
 a.class= 'prt';   
 document.body.insertBefore(a, document.body.lastChild);
}
</script>
Chadwick
that should read ` a.className= 'prt'; ` - `class` is a reserved word and might lead to problems when used as an unquoted property name
Christoph