• Ranges
    • Ranges
      • Ranges
      • DEVHIDE
        • Home (current)
        • About
        • Contact
        • Cookie
        • Home (current)
        • About
        • Contact
        • Cookie
        • Disclaimer
        • Privacy
        • TOS
        Login Or Sign up

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

        1.9k Views Asked by ClarkeyBoy At 28 January 2011 at 06:25 2025-12-06T00:28:00.027000

        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 best solutions below

        6
        roflwaffle roflwaffle On 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 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 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

        • Uncaught TypeError: undefined is not a function
        • Stop siblings replacing order of canceled element with jQuery-ui
        • jQuery mobile Multiselect doesn't update selected attribute
        • jQuery autocomplete display worng search item
        • List of conflicts between bootstrap and jquery-ui?
        • Creating boundries in JQuery/javascript
        • Having trouble focusing on the TAB that I was currently working on
        • How can i show a jquery menu which opens from right to left side?
        • Can't toggle from right to left Jquery
        • undefined new items appear while reordering a list with ui-sortable and angularJS
        • Jquery Mobile: Horizontal Buttons, fill container
        • Is it possible to override defined options of the jQuery-ui sortable widget?
        • Quick way to clear only option available list on a multiselect with jQuery?
        • How to inject html into iframe, and display inside jquery-ui dialog
        • Show embedded select box on overflow, out of popup container

        Related Questions in JQUERY-UI-TABS

        • Having trouble focusing on the TAB that I was currently working on
        • jQuery tabs enable horizontal scroll
        • Tabs JQuery UI: not show graphic elements in inactive tab
        • Multiple jQuery UI Tabs on Page: Close all other tabs when another is opened
        • jQuery tabs: How to avoid display of tabs while loading?
        • JQUERY UI TABS - PreventDefault jumping
        • oscMax jquery nested tabs issue
        • How to make element inside table switch tabs using jquery ui tabs
        • Tabs, Hide Previous on first tab and next on last
        • How to trigger tab using jQuery
        • jquery-ui tabs getting anchor href with older jquery 1.8
        • External file does not load in jQuery Tabs
        • How to pre-load tab content (other view with table and data loaded from DB using Zend controllers), before displaying tabs ?
        • jQuery UI Tabs: Don't let user switch tabs if form input in active tab has content
        • Dynamic Jquery Tabs based on Php foreach

        Related Questions in JQUERY

        • How to sort these using Javascript or Jquery Most effectively
        • Ajax jQuery firing multiple time display event for the same result
        • .hover() seems to overwrite .click()
        • Check for numeric value with optional commas javascript
        • Extending Highmaps Side Effect
        • Array appending after each onclick and loop in javascript
        • how can i append part of a table based on how many tr it has?
        • Play multiple audio files in a slider
        • Remove added set of rows
        • Access property of an object of type [Model] in JQuery
        • AJAX PHP - Reload div after submit
        • proengsoft/laravel-jsvalidation ReferenceError: jQuery is not defined
        • when a checkbox is checked how to display a different hidden element using javascript
        • Get jquery error Uncaught RangeError: Maximum call stack size exceeded
        • Removing only the closest thead on table filtering

        Related Questions in JQUERY-CLICK-EVENT

        • Trying to attach a click event to a dynamically created image to trigger a modal to close
        • on "tap, click" event firing twice. how to avoid it?
        • Jquery: Different click and enter handlers
        • jQuery programatically click a tag that has javascript:void(0)
        • How do I stop my accordion from popping the first time it's clicked?
        • jQuery / jQuery UI - $("a").live("click", ...) not working for tabs
        • Prevent jQuery event queuing
        • Jquery dynamically assign same function with different parameters to
        • .trigger('click'); isn't triggering
        • jQuery click handlers don't always work in IE 7/8, using v1.5
        • jQuery pass event on to underlying element
        • trigger a href sip/tel link on button click jquery
        • Why won't jQuery click event fire?
        • Click event not working on span close button
        • Different Ways of Handling Click Event

        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)

        Popular # Hahtags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

        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?
        .

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Math
        • Aftereffectstemplates