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 7776681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:56:40+00:00 2026-06-01T17:56:40+00:00

What I want to do is to set options into plugin like you normally

  • 0

What I want to do is to set options into plugin like you normally would, but to also override those options with html5 data-attributes.

Here I have exactly that (jsfiddle), but there’s a slight problem with that.

JSFiddle Code:

(function($){
    $.fn.extend({
        test: function(options) {

            var defaults = {
                background: null,
                height: null
            };

            var options = $.extend( defaults, options);

            return this.each(function() {
                  var o = options;
                  var obj = $(this);
                  var objD = obj.data();


                    // background
                    if ( !objD.background) {
                        var cBG = o.background;
                    }
                    else {
                        var cBG = objD.background;
                    }

                    // Opacity
                    if ( !objD.height) {
                        var cH = o.height;
                    }
                    else {
                        var cH = objD.height;
                    }

                    obj.css({
                        background: cBG,
                        height: cH  
                    });

            });
        }
    });
})(jQuery);

$(document).ready(function() {
    $('.test').test({

        background: 'red',
        height: 400

    });
});

The method that I’m using in the jsfiddle bloats the code so much, even with just 2 different options that are also using data.

Question is: How could I achieve same end result with less code, to determine wether or not data should be used?



Here is the part of the code from the jsfiddle, that determines wether or not to use data.

// objD is obj.data();

// background
if ( !objD.background) {
    var cBG = o.background;
}
else {
    var cBG = objD.background;
}

// Opacity
if ( !objD.height) {
    var cH = o.height;
}
else {
    var cH = objD.height;
}

obj.css({
    background: cBG,
    height: cH  
});
  • 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-06-01T17:56:41+00:00Added an answer on June 1, 2026 at 5:56 pm

    I had a feeling that there might be a better solution, but I wasn’t able to get it working, until now.


    Of course I am no expert, but this seems to work and shortens the code even more, since it works the same way the plugin options normally do, it just adds the data-attribute in there.

    1. Default – Initially use default options.
    2. Custom – If set, use custom options to override defaults.
    3. Data – If set, use data to override both.

    http://jsfiddle.net/lollero/HvbdL/5/

    o = $.extend(true, {}, options, $(this).data() );
    


    Here’s another jsfiddle example.

    This one toggles the width of each box on click. The middle box has a data-attribute with bigger width value than the other divs.

    As an extra example, here’s the same thing with 1 more option added in to the mix.

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

Sidebar

Related Questions

i want set class for repeater when doPostBack for those dataID i wan but
I want to encode a set of configuration options into a long string of
I am trying to fill options into a listbox dynamically. I want to set
I want to create a set of options, that will be appended to every
I want set Listbox background to transparent but not working Is there any idea?
I use NERDTree plugin. Now i want to set the 'NERETreeHijackNetrw' option to value
I want to deploy my Symfony2 project into my server and capifony but I
I want to set up something like this in my environment. [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self
I want to set a menu option icon for a drawable that is in
I want set interfaceOrientation in one UIView as LandscapeLeft and Portrait in other UIView.

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.