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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:05:34+00:00 2026-06-11T10:05:34+00:00

Can somebody help me? The following code works, but unfortunately a function call to

  • 0

Can somebody help me?

The following code works, but unfortunately a function call to doSomething() is necessary:

<body onload=init()>
</body>

<script>
    function Startscreen()
    {
        this.doSomething(); //why is this call necessary and how else to solve it?
        this.startscreenhtml = 
            "   <form onsubmit=doSomething()>   " + 
            "       <input type=text />         " +
            "   </form>                         ";
        document.write(this.startscreenhtml);
    }
    Startscreen.prototype.doSomething = function()
    {
        alert(123)
    }
</script>

<script>
    function init()
    {
        var startscreen = new Startscreen();    
    }
</script>

I do want to keep function doSomething as a function of object Startscreen.

edit: i do want to keep using document.write

Preferrably i want to declare doSomething outside of the constructor as i am doing now, because i believe that otherwise if i would call new Startscreen multiple times, the same function is created multiple times.

However, i tried many things to make this work without the call to doSomething, including declaring the function inside the constructor, but nothing worked.
My guess is that the call is necessary to make the function known, because if the code is read top to down, im assigning it to ‘onsubmit’ before i declared it.

Does anybody know how to solve this?

  • 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-11T10:05:35+00:00Added an answer on June 11, 2026 at 10:05 am

    I don’t think that the code works as you think in the current form. The doSomething function will be called when you create the form, but it will not be called when you submit the form.

    To call the function from the onsubmit event, it has to be known in the global context, as the event handler is not called in the context of the Startscreen instance. With your current code it’s not possible to call the function for two reasons; it’s not possible to reach from the global context, and as there is no reference left to the object instance there isn’t even an instance left that you can use to call it.

    (You could get the function from the prototype, or create a new instance of the object, but then your setup with an object would be completely pointless.)

    If you want to keep the instance, you have to put it in a variable that survives the init function:

    var startscreen;
    function init() {
      startscreen = new Startscreen();    
    }
    

    Now the object will still be around when the onsubmit event handler is called, so you can reach the function via the variable:

    this.startscreenhtml = 
      "<form onsubmit="startscreen.doSomething()">" + 
      "<input type=text />" +
      "</form>";
    

    This is of course not ideal, as you couple the object to the variable, but I hope that you gained some knowledge about what’s happening, so that you can come forward.


    To use the method in the submit event without having a global variable to find it, you have to create an element so that you can hook up an actual function as the event handler. That way you can reach the method from the event handler:

    // make a local variable that you can capture in a closure
    var me = this;
    var frm = document.createElement("form");
    // make an anonymous function that will create a closure capturing "me"
    frm.onsubmit = function(){
      me.doSomething();
    };
    

    This means that you can’t use document.write to create the form (or you need to give each form a unique id so that you can get a reference to the created element and hook up the event afterwards).

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

Sidebar

Related Questions

Can somebody help me in converting the following code written using if-else to try/catch.
Can somebody help me in understanding how split in java works.I have the following
Can somebody help me to convert the following array to a hash with the
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
Sure it's a simple question, but I can't fix it, can somebody help me?
Can somebody help me find the mistake I am doing in evaluating following XPath
Can somebody please help me fix my code.? I can't see where I'm going
I'd like some help with the following jQuery code if possible... $(document).ready(function() { $('table.sortable').each(function()
Can somebody help me with the following problem, here is the input XML, the
Can somebody help me to suggest how can I get the play list from

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.