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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:58:27+00:00 2026-06-16T06:58:27+00:00

This is my HTML page: <head> <meta http-equiv=Content-Type content=text/html; charset=UTF-8 /> <title>Untitled Document</title> <script

  • 0

This is my HTML page:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<script type="text/javascript" src="jquery-1.8.2.min.js"></script>

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

<script type="text/javascript">

$(document).ready(function() {
function closecontactform(){
alert("closing the form");
};
});
</script>
</head>
<body>
<p id="elementname">Click me to call function</p>
</body>

This is in the myscripts.js file:

$(document).ready(function() {
$('#elementname').click(function() {
alert("click detected");
closecontactform();
}); 
});

My actual pages are quite a bit more complicated than this and include a lot of jquery but this is the basic problem showing only the code necessary. I want to be able to call a function from a separate .js file, whereas the function is defined in the main page. In this basic simple version I tried removing the $(document).ready(function()… and in this simple example that helps however I require that line in my more complicated pages since removing it seems to break everything else I have going on.

You’ll see the “click detected” gets called, but the “closing the form” does not.

Are you able to give me any pointers?

Thanks,

  • 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-16T06:58:29+00:00Added an answer on June 16, 2026 at 6:58 am

    You have a scope problem. You don’t need to wrap named function in $(document).ready().

    If wrapped within ready it’s scope is only available within that ready function due to closure within the function.

    Example:

    $(document).ready(function() {
        function doAlert() {
            alert('foo');
        } 
        /* can call function since scope in same parent function*/
        doAlert();
    }); 
    /* function not in scope, is undefined here*/
    doAlert();
    

    Refactor so function is globally available and will work if called from anywhere. Will work in both cases below:

    function doAlert() {
        alert('foo');
    }
    
    $(document).ready(function() {
        doAlert();
    });
    doAlert();
    

    Further note:

    Many people worry that functions using jQuery selectors and methods must be wrapped in $(document).ready(). Named functions outside of ready can contain all jQuery code… but… they need to be called after DOM is ready to be sure html exists

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

Sidebar

Related Questions

Below is my html <html> <head> <meta http-equiv=Content-Type content=text/html; charset=windows-1252> <title>New Page 1</title> <script
I have coded a html page as follows: <html> <head> <meta http-equiv=Content-Type content=text/html; charset=UTF-8
I am using the following html page: <html> <head> <title>AJAX Example</title> <meta http-equiv=Content-Type content=text/html;
This is my page code: <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Marketing Tracking</title> </head>
Take a look at this html: <head> <title>Test page</title> <script type=text/javascript> function submitForm() {
This is my html: <!DOCTYPE html> <html> <head> <meta http-equiv = Content-Language content =
I've a page like this: <html> <head> <style type=text/css> #mainDiv{ height: 100%; } #myDiv{
This is my page header: <!DOCTYPE html> <html> <head> <meta name=viewport content=width=device-width, initial-scale=1, minimum-scale=1.0,
I have this html file with a conditional comment. <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta http-equiv=Content-Type
I'm having trouble with UTF-8. common.jsp <%@ page language=java contentType=text/html; charset=UTF-8 pageEncoding=UTF-8%> typical.jsp <%@

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.