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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:13:54+00:00 2026-06-12T00:13:54+00:00

Say that I have a HTML that links to two scripts: … <script type=text/javascript

  • 0

Say that I have a HTML that links to two scripts:

...
<script type="text/javascript" src="general.js"></script>
<script type="text/javascript" src="specific.js"></script>
...

Each of the two scripts has it’s own jQuery’s .ready() defined.

general.js:

jQuery(function() {
  var foo;
  $('#btn').click(function() {alert(foo())});
}

specific.js:

jQuery(function() {
  foo = function() {alert("hello")};
  $('#btn').click(function() {foo()});
}

where #btn is a button element.

When I clicked on #btn I was expecting to see “hello” dialog, but instead, I got Uncaught TypeError: undefined is not a function in chrome developer tools.

My own understanding is that foo should have been assigned the function already, instead of being undefined, before it’s accessed by the click event. Obviously, my understanding is incorrect. Can someone please explain to me why it’s behaving the way it is?

  • 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-12T00:13:55+00:00Added an answer on June 12, 2026 at 12:13 am

    You’re declaring “foo” with var inside the first function. It’s therefore local to that function and not visible external to it.

    You could make “foo” global by declaring it outside the first “ready” handler.

    Global variables are kind-of undesirable; it might be nicer to keep the value as a “data” value on the element itself.

    $(function() {
       $('#btn').data('foo', 'some value');
       $('#btn').click(function() {alert($(this).data('foo'))});
    }
    

    That way the data is associated with the elements (though not directly on the DOM objects; jQuery keeps track of the values in an internal map to avoid DOM-related memory leaks) and you don’t pollute the global namespace.

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

Sidebar

Related Questions

Let's say I have a graphical effect that I want to be overlaying HTML-Text.
Let's say i have an index.php that has html in it that looks like
So let's say I have a view that access a date: <%= Html.TextBoxFor(model =>
Say that I have two figures in matplotlib, with one plot per figure: import
Say I have two buttons Add Link and Add Text. The Add Link button
Say I have a some links that uses the live method to load dynamic
Let's say I have some HTML like so: <div id=text>This <i>is</i> a <b>test</b>. Do
I have some HTML 5 code that starts out with two black canvases and
Is its possible to have a javascript file that is aware of two different
Say that I have an immutable Point class with x and y parameters, 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.