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

  • Home
  • SEARCH
  • 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 5990227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:12:47+00:00 2026-05-22T23:12:47+00:00

I have a simple link: <a href=# id=test>Test Link</a> I want to get an

  • 0

I have a simple link:

<a href="#" id="test">Test Link</a>

I want to get an alert whenever this link is pressed, so I add:

<script>
$('#test').click(function() { alert('clicked!'); } );
</script>

and it works fine, but when i move this code to a remote javascript file, it doesn’t work..

any idea why?

I’ve also tried this code:

$(document).ready(function() {
    $('#test').click(function() { alert('clicked!'); });
});
  • 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-05-22T23:12:47+00:00Added an answer on May 22, 2026 at 11:12 pm

    Your second example, using the ready function, should be working. Your first example should also work provided you include the script below the element with the ID “test” (the element has to already exist when your script runs, since you’re not waiting for DOM ready). In both cases, your script must be included below (after) the jQuery script.

    Example when you don’t use ready

    Example when you do use ready

    I’d check that your external file is actually getting loaded (look for 404 errors in the browser console).


    Update: From your comment below, the problem is that the “test” element doesn’t exist when you’re trying to hook up the handler. click only sets up the handler on the element if it already exists. If you’re creating the element later, you have three options (two of which are really the same):

    1. Use the code you already have, but run it after you’ve created the element (e.g., in the success callback of the ajax call you’re making).
    2. Use live, which basically hooks the click event document-wide and then checks to see if the element you tell it (“#test”, in this case) was clicked.
    3. Use delegate on the appropriate container (the element within which you’re adding “test”). delegate is a more targeted version of live.

    live and delegate are both examples of a technique called event delegation, which jQuery makes easy for you by providing those methods.

    See the links for further information and examples, but for example, suppose you’re going to be adding the “test” element to an element with the ID “target”. You’d use delegate like this:

    $("#target").delegate("#test", "click", function() {
        alert("Clicked");
    });
    

    That hooks the click event on “target”, but acts a lot like you’ve just magically hooked it on “test” as soon as “test” was added. Within your handler, this refers to the “test” element just as with click.

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

Sidebar

Related Questions

I have a real simple script, where the user clicks on a link, a
I have a simple jQuery click and scrollTO anchor script. I have another page,
I have a simple scenario, where two servers are connected through a gigabit link.
I have a simple little test app written in Flex 3 (MXML and some
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In general, is it a best practice to have simple POJO Java classes implement
I develop tools in Autodesk Maya. Many of the tools I build have simple
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple webform that will allow unauthenticated users to input their information,

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.