tags:

views:

64

answers:

4

Is there a way to assign attributes in a more compact manner I dont really want to use setAttribute as it seems to be buggy in ie8 This list is for all attributes so its quite long

else if(a=="textalign")
{
    e.style.textAlign="";
    e.align=v
}
+2  A: 

Use a class instead of giving all the attribute values.

.testClass 
{
   // set all attribute values here
}

e.className = "test";

See

element.className

rahul
cannot use classes - see below
Johnny Darvall
A: 

Use some framework such as JQuery, it takes care of all of your browser incompatibility issues. In JQuery you use the .css('attributeName', 'value')method.

Deniz Dogan
A: 

jQuery would make that easy with .attr({attr1: val, attr2: val}) etc. It would also shield you from many cross-browser compatibility bugs.

Reinis I.
I cannot use jquery
Johnny Darvall
+2  A: 
if(a=="textalign")
{
    e.style.textAlign="";
    e.align=v
}

I don't know why you are trying to set alignment via an HTML attribute rather than just using the CSS... this is much less reliable as there are many elements which have no align attribute. HTML align is also deprecated and should be avoided in general.

You don't say what the “other attributes” are that you might want to set. If you are talking specifically about HTML attribute properties it's easy to set them by a name in a string:

e[a]= v;

But then you need a to be the HTML attribute property name, which would be ‘align’ not ‘textalign’. It wouldn't do anything special to try to workaround CSS overrides like textAlign, because there is no automated way to do that, and the interaction between the deprecated HTML styling attributes and CSS is ill-defined. Stick to attributes or CSS (CSS is highly preferable); don't use both.

If you are talking about setting any CSS style property, as I might guess from the name being ‘textalign’, that's done similarly:

e.style[a]= v;

But then, again, you'd want to be using the exact style property name ‘textAlign’ not ‘textalign’.

If you want to set CSS style properties by their CSS name, like ‘text-align’, you could transform that to the DOM name automatically:

// convert foo-bar-baz to fooBarBaz
//
var doma= a.replace(/-([a-z])/g, function(m, g) {
    return g.toUpperCase();
});

e.style[a]= v;

If you really do need to use case-lossy names like ‘textalign’ you'd have to use a lookup of all property names you wanted to use to get the case back:

var propernames= ['textAlign', 'borderColor', 'paddingTop']; // etc

for (var i= propernames.length; i-->0;)
    if (propernames[i].toLowerCase()===a)
        a= propernames[i];

e.style[a]= v;

Forget setAttribute. It has nothing to do with style properties (it's a bug in IE6-7 that it even works on styles there), and you shouldn't use it on elements either for HTML documents, as there are other IE6-7 bugs to contend with there. Stick to the ‘DOM Level 2 HTML’ direct property access stuff, which is more reliable and easier to read.

bobince
Wow. That's a much longer answer than I would bother to write to a question that requires the answerer to guess the what the question is before answering :)
Tim Down
ok - sorry about confusion here. Im building a rich-text-editor that applies style attributes directly to an object in designmode - so i cant use classes. The tip on setAttribute is helpful - ta. I just did not want to have 80 else ifs as my function does attempt to set most of the style attributes of an object.So you say this will worka="fontsize"v="10pt"e=the-element-by-ide[a]=v;ore.style[a]=v;?
Johnny Darvall
Nearly, but the DOM e.style property is called ‘fontSize’. Again, everywhere the CSS has a hyphen in the style name, DOM has a capital letter.
bobince
Also ‘pt’ is a bad unit to use for anything but print-specific stylesheets! :-) Stick to ‘px’ for fixed-size fonts and ‘%’ for relative-size.
bobince
Probably not relevant for your rich text editor, but there are one or two exceptions to the CSS-hyphen-becomes-JS-capital rule: `float` -> `cssFloat` springs to mind.
Tim Down