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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:30:38+00:00 2026-06-06T02:30:38+00:00

At the moment I’m trying to figure out how use default and custom settings

  • 0

At the moment I’m trying to figure out how use default and custom settings in jQuery plugins. I want to be able to apply the plugin’s default settings to elements using a universal class, but additionally customise some of those elements by selecting their ID’s instead. Example: http://jsfiddle.net/srkPT/2/

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <title>Untitled Document</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <style type="text/css">
        .box {width:200px; height:200px; padding:50px; margin:40px; border:1px solid; float:left;}​
        </style>

        <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
        </head>

        <body>

       <div class="box">Box 1 - </div>

    <div id="one" class="box">Box 2 - </div>

    <div id="two" class="box">Box 3 - </div>

    <div class="box">Box 4 - </div>

        <script type="text/javascript">

    (function($) {


        $.fn.myPlugin = function(params) {

            var defaults = {

                background: '#ddd',

                content: 'foo'

            };

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

            return this.each(function() {

                $(this).css('background-color', params.background)

                .append(params.content + ' ');

            }); //RETURN this EACH

        }; //fn myPlugin

    })(jQuery);


        $(function(){

        $('.box').myPlugin();

            $('#one').myPlugin({content:'bar'});

            $('#two').myPlugin({background:'yellow'});

        });//document READY​

        </script>

        </body>
        </html>

What happens when this runs is that it appends 2 lots of content to #one and #two, and I can understand why; the plugin triggers on all elements with .box (including #one and #two), and then it runs a second time on divs #one and #two, appending the new content on top.

Without changing or running filters on selectors, or adding extra classes to the html, is there any practical way to get around this, so it doesn’t apply the plugin twice to the same element? Thanks in advance.

Update

Got this working thanks to a suggestion by czarchaic. Example: http://jsfiddle.net/srkPT/3/

However, it is dependant on the plugin being applied in a specific order. For now, I’m pretty happy, though if anyone can think of a way to get the desired effect regardless of which order it’s applied, I’d be most grateful. Thanks to everyone for their help.

  • 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-06T02:30:41+00:00Added an answer on June 6, 2026 at 2:30 am

    You could set a data attribute and test for it.

    $.fn.myPlugin=function(){
      return this.each( function(){
        if ( $( this ).data( 'test' ) ){
          return false;
        }
        $( this ).data( 'test', true );
        // plugin code
      });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

at the moment i am trying to write a unreal amount of data out
I am at the moment trying to learn how to use basic grid view
At moment I want to implement picture upload without using any plug-ins. My upload
At the moment I am trying to HTTP response request in my Sharepoint WarmUp
At the moment, I'm fading components in/out at a rate of 0.1 alpha/second .
At the moment I am trying to validate a form using PHP. The problem
Having a duh moment trying to implement a new content page Here's the structure
At the moment i'm overriding drawRect in a view in order to be able
At the moment I am in the following /public_html/v3/football/admin/new.php Inside that new.php, i want
At the moment we use HSQLDB as an embedded database, but we search for

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.