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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:46:15+00:00 2026-05-21T08:46:15+00:00

I’m pretty new to javascript and jquery, and have run into a problem that

  • 0

I’m pretty new to javascript and jquery, and have run into a problem that I have not yet found a solution for. I’ll attempt to paint it in a fairly simple way.

I have a page that offers the user a few choices (its a game, so..) like attack or defend, what weapon to use. It then loads two html ‘s, each with a “commit” button. The commit buttons are tied to (separate) functions that pass data to ajax.

The problem I have is that I’m getting buried in nested functions. Does that make sense? My code looks something like this:

code:

$(function() {
  $('#choice1').click(function() {
        //do some stuff
  });
  $('#choice2').click(function() {
         //do some stuff
   });
  $('#choice3').click(function() {
        //do some stuff, including creating the choices below, and their submit boxes
        $('#subChoice1').click(function() {
              //send data with ajax to a php document.  get result
              //create some text on my document to display results
              //create an input button, along with an id="subButton1"
        });
        $('#subChoice2').click(function() {
              //send data with ajax to a php document.  get result
              //create some text on my document to display results
              //create an input button, along with id="subButton1"
              //(yes, feed both back to same func)
        });
  });  // end choice3
  $('#subButton1').click(function() {
              //my buttons in the subChoices do not call this function(!!??)
  });
});

edit: link no longer works, sorry. And I’ve created a living example here.

Thanks for any tips or pointers. I’m almost certain that there is just something simple that I’m missing or unaware of that will get me on the right track.

  • 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-21T08:46:15+00:00Added an answer on May 21, 2026 at 8:46 am

    You could always use normal named functions instead of inline anonymous ones. That might help if the mountain of functions is getting too tall.

    Update:

    Here’s your working Case 1:

        $('#button1').click(function(){
            $('#div1').append('<span id="span1"><br>Alright. Button 1 worked.  Here\'s another button.</br></span>');
            $('#div1').append('<input type="button" value = "Button 2" id="button2"/>')
    
            $('#button2').click(function() {
                $('#div1').append('<span id="span1"><br>Hey!  Button 2 worked!.  Let\'s keep going.</br></span>');
                $('#div1').append('<input type="button" value = "Button 3" id="button3"/>')
    
                    $('#button3').click(function() {
                        $('#div1').append('<span id="span1"><br>Hey!  Button 3 worked!.  That\'s probably enough.</br></span>');
    
                    });
            });
    
        });
    

    What I’m proposing is to update it like this:

        function button3_click() {
            $('#div1').append('<span id="span1"><br>Hey!  Button 3 worked!.  That\'s probably enough.</br></span>');
        }
    
        function button2_click() {
             $('#div1').append('<span id="span1"><br>Hey!  Button 2 worked!.  Let\'s keep going.</br></span>');
             $('#div1').append('<input type="button" value = "Button 3" id="button3"/>');
             $('#button3').click(button3_click);
        }
    
        function button1_click() {
            $('#div1').append('<span id="span1"><br>Alright. Button 1 worked.  Here\'s another button.</br></span>');
            $('#div1').append('<input type="button" value = "Button 2" id="button2"/>')
            $('#button2').click(button2_click);
        }
    
        $('#button1').click(button1_click);
    

    That way the structure of the logic stays the same, but you can pull out the nested function definitions.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

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.