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

  • Home
  • SEARCH
  • 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 6915715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:30:35+00:00 2026-05-27T09:30:35+00:00

I use jQuery’s .css() method to apply styles to an element. I do this

  • 0

I use jQuery’s .css() method to apply styles to an element. I do this like so:

var cssObj = {
    'background-color' : '#000',
    'background-image': '-webkit-linear-gradient(top,#000,#fff)',
    'background-image': 'linear-gradient(top,#000,#fff)'
};

$(".element").css(cssObj);

The problem with this is that obviously I use duplicate keys in the object, which is not cool.

How can I solve this problem? I need to pass the CSS params with duplicate names to address most browsers.

  • 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-27T09:30:36+00:00Added an answer on May 27, 2026 at 9:30 am

    Having multiple keys with the same name is not valid, and will generate an error in strict mode.

    Create a function/plugin which applies the properties of your cssObj. If a string-string pair is found, set a CSS property with the desired value.
    If an array is found, loop through it, and update the property with each value. If an invalid value is found, it’s ignored.

    DEMO: http://jsfiddle.net/RgfQw/

    // Created a plugin for project portability
    (function($){
        $.fn.cssMap = function(map){
            var $element = this;
            $.each(map, function(property, value){
                if (value instanceof Array) {
                    for(var i=0, len=value.length; i<len; i++) {
                        $element.css(property, value[i]);
                    }
                } else {
                    $element.css(property, value);
                }
            });
        }
    })(jQuery);
    
    // Usage:
    var cssObj = {
        'background-color': '#000',
        'background-image': ['-webkit-linear-gradient(top,#000,#fff)',
                             'linear-gradient(top,#000,#fff)']
    };
    $(".element").cssMap(cssObj);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use jQuery to get the browser version like this: var x = $.browser.version;
I use jquery for adding table dynamically and I use this pattern: var tbl
I use jQuery fadeTo to get a transparent background of a DIV, but this
I use jQuery to block ui like this $('#send').click(function() { $.blockUI({ message: '<p id=loading><img
I use jQuery UI dialog without theme. It shows like this: Now I would
I use jQuery to do AJAX calls. But specialchars like ÆØÅ (danish letter) comes
I use jquery ui to apply a drag and drop effect on a serial
I use jQuery ui autocomplete feature. var colors; $(document).ready(function(){ loadColors(); $('#empf').autocomplete(colors); } function loadColors(){
I use jQuery and for things like setting toggles, I find myself writing a
I use jquery to post data to mysql. **In settings.php i have this short

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.