• Ranges
    • Ranges
      • Ranges
      • TechQA.

        jQuery / jQuery UI - $("a").live("click", ...) not working for tabs

        1.9k views Asked by ClarkeyBoy At 2011-01-28T06:25:40+00:00 28 January 2011 at 06:25 2025-12-23T19:20:34+00:00

        So I have some jQuery UI tabs. The source code is as follows:

        HTML:

        <div class="tabs">
            <ul>
                <li><a href="#ranges">Ranges</a></li>
                <li><a href="#collections">Collections</a></li>
                <li><a href="#designs">Designs</a></li>
            </ul>
            <div id="ranges"></div>
            <div id="collections"></div>
            <div id="designs"></div>
        </div>
        

        jQuery:

        $(document).ready(function() {
            $(".tabs").tabs();
        });
        

        My problem is that I am trying to make each tab load a page into the content panel on click of the relevant link. To start with I am just trying to set the html of all the panels on clicking a link. From the code below, if I use method 1, it works for all links. However if I use method 2 it doesn't - but only for the links in the tabs (i.e. the labels you click to select a tab).

        Method 1 (works for all links all the time, but would not be applied to links which are added after this is called):

        $("a").click(function () {
            $("#ranges, #collections, #designs").html("clicked");
        });
        

        Method 2 (works for all links which are not "tabified"):

        $("a").live("click", function () {
            $("#ranges, #collections, #designs").html("clicked");
        });
        

        Does anyone know why it is behaving like this? I would really like to get method 2 working properly as there may well be links which I need to add click events to which are added after the page is originally loaded.

        Thanks in advance,

        Richard

        PS yes the function calls for .live and .click are both in the $(document).ready() function, before anyone says that that may be the problem - otherwise it wouldn't work at all..

        Edit:

        The solution I came up with involves an extra attribute in the anchors (data-url) and the following code (which outputs a 404 not found error if the page cannot be loaded). I aim to expand this over the next few weeks / months to be a lot more powerful.

        $(".tabs").tabs({
            select: function (event, ui) {
               $(ui.panel).load($(ui.tab).attr("data-url"), function (responseText, textStatus, XMLHttpRequest) {
                   switch (XMLHttpRequest.status) {
                       case 200: break;
                       case 404:
                           $(ui.panel).html("<p>The requested page (" + $(ui.tab).attr("data-url") + ") could not be found.</p>");
                           break;
                       default:
                           $(ui.panel).html("<p title='Status: " + XMLHttpRequest.status + "; " + XMLHttpRequest.statusText + "'>An unknown error has occurred.</p>");
                           break;
                   };
               });
           }
        });
        
        jquery-ui jquery-ui-tabs jquery jquery-click-event
        Original Q&A
        3

        There are 3 answers

        6
        roflwaffle roflwaffle On 2011-01-28T06:36:21+00:00 28 January 2011 at 06:36 BEST ANSWER

        I don't know if I understand what you are going for but basically you want to do something once a tab is clicked? Here's the docs for setting up a callback function for selecting a tab.

        EDIT: Don't know if that link is working correctly, you want to look at select under Events. But basically it is:

        $("#tabs").tabs({
            select: function(event, ui) { ... }
        });
        

        Where ui has information on the tab that was clicked.

        3
        Betamos Betamos On 2011-01-28T06:36:36+00:00 28 January 2011 at 06:36

        Maybe the tabs() plugin that you are using is calling event.preventDefault(); (Reference) once it has created it's tabs.

        Then it captures the click event and the bubbling stops, so it doesn't invoke your click-function. In jQuery this is done with

        $(element).click(function(){
            // Do stuff, then
            return false; // Cancels the event
        });
        

        You'd have to alter the tabs() plugin code and remove this return false; statement, OR if you are lucky, the plugin might have an option to disable that behavior.

        EDIT: Now I see you're using jQuery UI. Then you should check the documentation there, since it is an awesome plugin, it will do anything you want if you do the html right and pass it the right options.

        0
        scurker scurker On 2011-12-15T14:49:39+00:00 15 December 2011 at 14:49

        jQuery UI tabs has an outstanding issue where return false; is used instead of event.preventDefault();. This effectively prevents event bubbling which live depends on. This is scheduled to be fixed with jQuery UI 1.9 but in the meantime the best approach is use the built in select event as suggested by @rolfwaffle.

        Related Questions in JQUERY-UI

        • jquery__WEBPACK_IMPORTED_MODULE_13__(...).sortable is not a function
        • Jquery autopopulate not displaying
        • jQuery plugin is not shown
        • How to upgrade blueimp jquery file upload to use jquery-widget 1.13
        • Set value JQUERY UI Slider with LeaFlet map
        • trying to delete existing tabs which were dynamically added
        • Jquery UI sortable addon with React not working
        • Angular.js directive does not work for uib-tooltip
        • tooltip for input fields not loading
        • How to install jq.Schedule in Vue3 + Typescript
        • JQuery - Drag, Drop and Sortable
        • tabs getting adding again and again
        • JQuery UI remove tooltip on successful input
        • jQuery Autocomplete Firing to Wrong URL
        • How to add total count in centre of the doughnut chart and bottom left and right value using Chart.js?

        Related Questions in JQUERY-UI-TABS

        • Dynamically created jQuery Tabs on ColdFusion Pages
        • jquery tabs sometimes set the iframe height to zero
        • How to activate the parent tab content using javascript?
        • jQuery UI tabs using aria-controls instead of title to have a single panel
        • Remember last clicked tab on page refresh
        • Uncaught Error: Method load does not exist on jQuery.tabs
        • How to integrate JqueryUI Tabs in Blazor webAssembly application
        • Load Php Page Inside Jquery Chrome Tabs
        • Jquery tab navigation not working properly
        • jquery-ui tab "Interactive controls must not be nested"
        • Jquery tab not showing first child selected
        • Reactjs Dynamically call components having cards under tabpanels using array
        • How to manipulate Bootstrap tabs from another page using Ajax
        • link to a page with tabbed content doesnt work
        • I want a single page scroll with top nav menu scroll and active (Active menu should be in first position scroll)

        Related Questions in JQUERY

        • In Datatables, start value resets to 0, when column sorting
        • Bootstrap modal not showing at the desired position on a web page when the screen size is smaller
        • window.location.href redirects but is causing problems on the webpage
        • Using JQuery Date Slider
        • Storing selected language in localStorage
        • How to stop other divs from still showing when i click a different button?
        • Check multiple values with jQuery
        • Bootstrap component does not want to render in Datatables function
        • put white spaces when entering an amount moneytype symfony
        • Trouble accessing custom header in AJAX response using jQuery in Fiware Keyrock
        • I just cant make it work, HTML, JS and Firebase error
        • Didn't declared variable still not getting any error in JavaScript
        • Move element horizontally while scrolling vertically in pure JavaScript
        • allow multi carousel in same page
        • Embedded TikTok posts / thumbnail styling issue

        Related Questions in JQUERY-CLICK-EVENT

        • It doesn't call the function but it jumps to the top of clickevent instead?
        • Boolean returned by Web Method and added to row as data attribute is undefined
        • Jquery - Click event on resize() not work properly
        • jquery toggle is working only one time and not closing back
        • Google Analytics 4 - Events are not being tracked
        • In Backbone how to get click event on an anchor tag within a list item
        • jQuery .click() event not working. Out of two one is working the other does not
        • Click event not working on span close button
        • How to make image/div smaller when LMB is being pressed?
        • JQuery clearing all text boxes
        • How to reopen a closed click event in jQuery without reload?
        • Can the effect of physical click always be achieved through javascript events?
        • Trying to make a Alert button in tampermonkey
        • I want to set my checkbox on "checked" when i click a specific td in my table
        • Extra click after dynamically added HTML elements via jQuery

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)
        • Privacy
        • Terms
        • Cookies
        • Homegardensmart
        • Math
        • Aftereffectstemplates