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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:19:43+00:00 2026-06-10T06:19:43+00:00

I ran into a problem with jQuery selectors when setting up an ajaxnavigation for

  • 0

I ran into a problem with jQuery selectors when setting up an ajaxnavigation for a site.
The site is based on twitter bootstrap and uses the nav-bar and dropdowns provided by the framework.

I use hashchange events to load new content into section#main when a link with class="ajaLink" is clicked.

I got stuck as it seemed the event wouldn’t bind to the links in the twitter bootstrap dropdown menu. I investigated the JS for the dropdowns but I couldn’t see anything that would prevent the event from being bound and triggered.

In frustration I chanegd the selector from

$(document).on('click', '.ajaxLink', function (e) {
    ...
}

To

$('.ajaxLink').on('click', function (e) {
    ...
}

This made the event bind to all the links in the nav-bar and dropdowns, but would leave the links in the dynamically loaded content.

I can’t figure out the difference between the two selectors and why I get this behaviour.
My solution so far is to use both the selectors, but this doesnt feel as a solution as long as I cant tell for sure the event wont bind twice to the same element.

So question is what is the difference between the two selectors used?
How would I rewrite the selector to make sure I bind each .ajaLink element only once?

The HTML…

<!DOCTYPE html>
<html>
<head>


</head>
<body>
    <div class="container">
        <header>
            <div class="navbar navbar-inverse">
                <div class="navbar-inner">
                    <div class="container">
                        <!-- Minimizing the menu under a button when screen gets too small. -->
                        <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                            ...

                        </a>

                    <div class="nav-collapse">
                    <ul class="nav">
                        <!-- Choose application. -->
                        <li class="dropdown">
                            <a class="dropdown-toggle" href="#" data-toggle="dropdown"> Menu 1 <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li class=""><a class="ajaxLink" href="/Blabla">Blabla</a></li>
                                <li class=""><a class="ajaxLink" href="/Yadayada">Yadayada</a></li>
                                ...
                            </ul>
                        </li>
                        <!-- Choose application. -->
                        <li class="dropdown">
                            <a class="dropdown-toggle" href="#" data-toggle="dropdown"> Menu 2 <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li class=""><a class="ajaxLink" href="/Blabla2">Blabla 2</a></li>
                                <li class=""><a class="ajaxLink" href="/Yadayada2">Yadayada 2</a></li>
                                ...
                            </ul>
                        </li>
                    </ul>
                    <form class="navbar-search pull-right" action="/Home/Search" method="post" >
                        <input type="search" class="search-query" placeholder="Sök" />
                        <button type="submit" class="btn btn-inverse" id="search-button ">Search</button>
                    </form>


                  </div><!--/.nav-collapse -->
                </div>
              </div>
            </div>
        </header>
        <div class="log label label-important" style="display:none;" ></div>
    <section id="main" style="opacity: 1; ">

    <div class="row-fluid">   
    <div class="span2">

    </div> 
    <div class="span10">
        <span class="gradientlabel"><a href="#">Artiklar</a></span> 

        <table class="table table-striped table-bordered table-condensed overview-table">
            <tbody><tr>
                <th>
                    Art No.
                </th>
                <th>
                    Description
                </th>
                <th>
                    Price
                </th>
                <th></th>
            </tr>

            <tr>
                <td>
                    16791
                </td>
                <td style="overflow:hidden;white-space: nowrap;">
                    175/70-13 82Q Semperit Ice Grip 2 Dubbat
                </td>
                <td>
                    300
                </td>
                <td>
                    <a class="ajaxLink" href="/Artiklar/Edit/16791">Edit</a> 
                </td>
            </tr>
            <tr>
                <td>
                    16792
                </td>
                <td style="overflow:hidden;white-space: nowrap;">
                    195/55-15 85Q Uniroyal Nordic Dubbat
                </td>
                <td>
                    550
                </td>
                <td>
                    <a class="ajaxLink" href="/Artiklar/Edit/16792">Edit</a> 
                </td>
            </tr>
            ...
        </tbody></table>
    </div>
    </div>
    </section>
        <footer>
        </footer>
    </div>

</body>
</html>

The Javascript

//Add event for all ajaxLink (except for the ones in the bootstrap dropdown)
$(document).on('click', '.ajaxLink', function (e) {
    var self = $(this);
    if (self.attr('href').length > 2) {
        window.location.hash = "!" + self.attr('href');
    }
    return false;
});

//I dont really know yet, but this is the only way to attach the event to bootstraps dropdown, and also have to close the dropdown programmatically.
$('.ajaxLink').on('click', function (e) {
    var self = $(this);
    //if (self.prop('tagName') != 'A') { alert('not a link'); self = self.find('a:first'); }
    if (self.attr('href').length > 2) {
        window.location.hash = "!" + self.attr('href');
    }
    self.closest('.dropdown').removeClass('open');

    return false;
});
  • 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-10T06:19:45+00:00Added an answer on June 10, 2026 at 6:19 am

    The difference is that $('.ajaxLink').on('click', function (e) { ... } only binds elements that already exists.

    $(document).on('click', '.ajaxLink', function (e) { ... } has an delegate and also binds .ajaxLink elements that are created dynamically in the future.

    If you want an delegate you really should use a parent that is closer to the child elements than document. The reason being that is saves unnecessary searches in the DOM tree, which gives better performance.

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

Sidebar

Related Questions

Never ran into this problem with jQuery before. I have the following: $(document).ready(function() {
I ran into a problem loading the admin section of my django site with
I was wondering if anyone else ran into the problem where jQuery scripts such
I ran into a problem on Chrome and Safari using jQuery to do some
I've ran into the following problem: the javascript lib i'm working on uses JSON
Ran into problem with creating custom select dropdown plugin in jQuery. I'm at the
im currently learning jQuery and have ran into a problem. I am trying to
haven't done much jquery and ran into a problem. I'd like to bind hover
I'm starting out on jquery and ran into a small problem where $.ajax(...) is
I've ran into a wierd problem.. I'm doing some custom button-styling using jQuery and

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.