Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 405725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:25:37+00:00 2026-05-12T17:25:37+00:00

Is there a way to assign attributes in a more compact manner I dont

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:25:37+00:00Added an answer on May 12, 2026 at 5:25 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to assign or define group within html markup?
Is there a way to to evaluate a boolean expression and assign its value
Is there an easy way in Visual Studio to assign default usings when i
Is there a way to assign the values of a list to a list
is there a way to assign a special value when a binding cannot be
Is there a way to assign a specific view to search results in drupal
I want to assign HTML attributes to forms created with Html.BeginForm in ASP.NET MVC
Is there a way to assign tasks to developers using TODO: statements in Mylyn
is there way how to get name ov event from Lambda expression like with
Is there way to better identify design pattern in source codes, esp. if you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.