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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:42:15+00:00 2026-06-06T11:42:15+00:00

I have an easy one SO. Why cant I attach a click event straight

  • 0

I have an easy one SO. Why cant I attach a click event straight to an anchors Id? I should have pointed out that I am also using JQuery Mobile.

            <div id="foobarNavbar" data-role="navbar" style="display:none;">
                <ul>
                    <li><a id="foo" href="#foo" data-icon="plus">New Event</a></li>
                    <li><a id="bar" href="#bar" data-icon="grid">Events</a></li>
                </ul>
            </div><!-- /foobarNavbar-->

I am trying to attach a click event to foo. This doesn’t work:

        $('#foo').bind('click', function(e) 
        {   
            e.preventDefault();
            console.log("You clicked foo! good work");
        });

This does work but gives me the click event for both foo and bar. Is it not possible to bind to an anchor Id or am I making a rookie error?

        $('#foobarNavbar ul li a').bind('click', function(e) 
        {   
            e.preventDefault();
            console.log("You clicked foo! good work");
            console.log(e);
        });
  • 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-06T11:42:16+00:00Added an answer on June 6, 2026 at 11:42 am

    Important: Use $(document).bind(‘pageinit’), not $(document).ready()

    The first thing you learn in jQuery is to call code inside the
    $(document).ready() function so everything will execute as soon as the
    DOM is loaded. However, in jQuery Mobile, Ajax is used to load the
    contents of each page into the DOM as you navigate, and the DOM ready
    handler only executes for the first page. To execute code whenever a
    new page is loaded and created, you can bind to the pageinit event.
    This event is explained in detail at the bottom of this page.

    I was trying to bind using document ready instead of pageinit. The first function

    $('#foo').bind('click', function(e) 
            {   
                e.preventDefault();
                console.log("You clicked foo! good work");
            });
    

    works fine when moved to the ‘pageinit’ event. I am still not sure, however, why the second code example worked but not the first.

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

Sidebar

Related Questions

here's an easy one (that I'm struggling with)! I have a textarea, a button,
O.K. I think that's an easy one! I have a ViewMasterPage with some links
This is a fairly easy one, but I cant seem to figure it out.
An easy one, but I can't figure it out. I have a link called
I'm sure this is an easy one for someone out there. I have a
My apologies if this is an easy one. I have Googled it up the
I have two objects - one that contains some code with will fire an
I think this will be a really easy one for anyone that is decent
I thought this would be an easy one for me to figure out (or
Seems like this should be easy or at least documented somewhere, I just cant

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.