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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:08:01+00:00 2026-06-15T03:08:01+00:00

ok so i created a function in jQuery called $.fn.customTabs and i am setting

  • 0

ok so i created a function in jQuery called $.fn.customTabs and i am setting variables inside of it to certain elements within the DOM. when i call it the second time, the variables are being set to different DOM elements and effecting my first call to the function.

Any idea how to have certain instance variables with in each function call?

EDIT

$.fn.customTabs = function(data,user_options) {
    $this = $(this);
    alert($this.attr('id'));
    var options = 
        {
            speed : 750,
            startIndex : 0,
            duration : 10000,
            height : "300px"
        },
        final_options = {
            "width" : "100% - 5px",
            "position" : "relative",
            "margin" : "0",
            "padding" : "5px"
        };
    $.extend(options,user_options);
    var tabPadding = parseInt(final_options.padding,10);
    var cssTabHeight = parseInt(options.height,10) - tabPadding + "px";
    $this.css(final_options);
    var num = data.length - 1;
    if(data) {
        $container = $('<ul class=\"tabsContainer\">').css({
            height : options.height,
            position : "relative",
            "list-style" : "none",
            padding : "0",
            margin : "0"
        });
        for(var count = 0; count <= num; count++) {
            $object = data[count];

            $tab = $('<li id=\"tab_' + count + '\" class=\"tab\">').css({
                height : cssTabHeight
            });

            if($object.element) {
                $object.element.css({
                    margin : "0",
                    padding : $object.element.css('padding') || "0"
                });
                if($object.element.is('a')) {
                    $tab.css({
                        "text-align" : "center" 
                    });
                    $object.element.find('img').css({
                        "max-height" : "100%",
                        "max-width" : "100%"
                    });
                } else if($object.element.is('img')) {
                    $tab.css({
                        "text-align" : "center" 
                    });
                    $object.element.css({
                        "max-height" : "100%",
                        "max-width" : "100%"
                    });
                } else {
                    $object.element.css({
                        width : "100% - " + tabPadding,
                        height : "100%"
                    });
                }
                $object.element.appendTo($tab);
            } else {

            }

            $container.append($tab);
        }
        $tabWrapper = $('<div>').html($container).css({
            overflow: "hidden"
        });
        $this.html($tabWrapper);
    } else {

    }

    var tabIndex = options.startIndex,
        x,
        tabHeight = parseInt(cssTabHeight,10) + tabPadding;

    var interval = setInterval(function() {
        makeInterval();
    },options.duration);

    $('.tabMenuButton').click(function() {
        clearInterval(interval);
        var element = this;
        var index;
        $('.tabMenuButton').each(function(i, ele) {
            if(ele == element) {
                index = i;
            }
        });
        var diffrence = index - tabIndex;
        if(diffrence < 0) {
            x = "-=" + (tabHeight*parseInt(diffrence,10));
            changeTab(x);
        } else if(diffrence > 0) {
            x = "-=" + (tabHeight*diffrence);
            changeTab(x);
        }

        tabIndex = index;
        interval = setInterval(function() {
            makeInterval();
        },options.duration);
    });

    var makeInterval = function() {
        if(tabIndex != num) {
            tabIndex++;
            x = "-=" + tabHeight;
        } else {
            tabIndex = 0;
            x = "+=" + (tabHeight*num);
        }

        changeTab(x);
    }

    var changeTab = function(distance) {
        $container.animate({
            "top" : distance + "px"
        }, options.speed);
    }

    return $this;
}

after the second call to this function is made, the $container is being set to the element created in the second call for both Intervals.

BTW this code works just gets messed up when there are two of them on the same page.

  • 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-15T03:08:02+00:00Added an answer on June 15, 2026 at 3:08 am

    (…) when i call it the second time, the variables are being set to different DOM elements and effecting my first call to the function. Any idea how to have certain instance variables with in each function call?

    The problem you are facing is the usage of global variables in places where you should use local ones. As a result, all the elements on which you execute the jQuery function, share the same variables.

    These variables are:

    • $this,
    • $container,
    • $object,
    • $tab,
    • $tabWrapper,
    • and possibly other.

    So it looks like you are treating normal variables with names beginning with $ as if they were special, automatically local. They are not local: you are just creating global variables with dolar sign at the beginning of their names.

    There are two solutions for you:

    • add var keyword in places where you first define it, or even put all variables at the beginning of appropriate functions, with var keyword in front of them, or
    • follow the guide of code quality tools such as JSLint: http://www.jslint.com/,

    And remember: your jQuery function must work independently between elements, so you must not use global variables. Instead, if you need to somehow store data between calls or for some reason within the code, use one of two options:

    • use closures (storing variables in outer scope, yet within the scope of jQuery function),
    • use jQuery’s .data() to attach data to specific elements (use your own function-specific naming to not interfere with other functions and/or plugins used by the user of your script),
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have created one jQuery function to check if elements exist or not
I've created a custom jQuery function and I want to call it for every
Sometimes this function can be called too quickly and multiple elements are created but
I created a jQuery function that calls on a PHP server-side file that pulls
I've created a wrapper function for jQuery's $.ajax() method so I can pass different
How do you create a function in jQuery, call it, and pass a variable
This is my current flow of JQuery: Object is created. Object has a function
I created a simple a jQuery function that is to be used for developer
I have created a function called billingOptions() it is associated with a dropdown (select)
I have an object called ValueBox that I created like this: function ValueBox(params) {

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.