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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:50:22+00:00 2026-06-04T08:50:22+00:00

I have a simple script which calls a function that is set in the

  • 0

I have a simple script which calls a function that is set in the function that the call is made…

But i get an “undefined function” error.

My script is:

function messages_document(messages){
    messages = JSON.parse(messages);

    function del_msg(id){
        result = call_file('del.php',id);
        if(result){
            messages[id].length = 0;
        }
    }

    var output = [];
    output.push('<p align="center"><b><u>My Messages</u></b></p> <br/><br/>');

    for(var id in messages){
        output.push('<a href="#" onclick="del_msg('+id+')">Delete Message</a>');
    }

    document.getElementById('main').innerHTML = (output.join(''));
}

I’m curious if i have misunderstood how scope works because i get:

del_msg is not defined

Any ideas why this 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-04T08:50:23+00:00Added an answer on June 4, 2026 at 8:50 am

    Function statements are defined in the current scope, not globally. In this case, that means that del_msg exists only within the scope of messages_document. However, you invoke it via an onclick attribute: these attributes are evaluated globally. Since del_msg doesn’t exist globally, it doesn’t work.

    You have two options. One is to make del_msg global, by defining it outside any other functions. However, this pollutes the global namespace, generally a bad thing. Better would be to apply it to your a element within the scope. This is going to require building the elements via DOM methods, rather than by HTML

    var para = document.createElement('p'),
        id,
        link;
    para.innerHTML = '<b><u>My Messages</u></b></p> <br/><br/>';
    para.align = 'center';
    
    for(id in messages){
        link = document.createElement('a');
        link.href = '';
        link.onclick = del_msg;
        link.innerHTML = "Delete message";
        para.appendChild(link );
    }
    
    document.getElementById('main').appendChild(para);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple script that calls a function in a while loop.
I have a very simple python script that should scan a text file, which
i have this simple script: function paginateUsers(page){ get( _config_remote_host+'/users?page='+page,function(json){ json = JSON.parse(json); var _html
I have an AJAX request (using JQuery) that calls a PHP script which does
I have a script in Filemaker that calls a Brian Dunning hosted custom function
I have a simple slideshow script which rotates images at a given interval. It
I have a simple Perl script which runs as a Linux daemon using an
Here I have a simple php script which displays some values from a database
I have a simple php script on my web server which I need to
I think this is quite simple. I have a working AJAX Jquery script which

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.