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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:45:10+00:00 2026-06-10T05:45:10+00:00

My objective is fairly simple. In my webapp, I have two pages and I

  • 0

My objective is fairly simple. In my webapp, I have two pages and I want to call each one from the other page.

But the way I have implemented the function calls in Javascript seems to be flawed. I have read up on closure, bubbling, recursion and event.stopPropagation(), but still not sure what the right way to go about implementing this.

Here’s the smallest code that I could reproduce my problem in.

    function init(){
    var $div1 = 'Click to Load page 2';
    $("#main").append($div1);

    var $div2 = 'Click to go back to  page 1';
    $("#main").append($div2);    
    displayFirstPage();
}


    function displayFirstPage() {
    var $div1 = $("#div1");
    var $div2 = $("#div2");    

    $div2.hide();
    $div1.show();

    alert("first called");

    $div1.click(function(){
    displaySecondPage();
    });
}


    function displaySecondPage() {
    var $div1 = $("#div1");
    var $div2 = $("#div2");    

    alert("second called");
    $div1.hide();
    $div2.show();

    $div2.click(function(){
    displayFirstPage();
    });
}

After clicking the divs a few times, I end up with numerous alert’s popping up. I just want each call to be executed once. Clearly, I am missing the way to terminate the function call.

Any help appreciated.

  • 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-10T05:45:11+00:00Added an answer on June 10, 2026 at 5:45 am

    Your

    $div1.click(function(){
        displaySecondPage();
    });
    

    and

    $div2.click(function(){
        displayFirstPage();
    });
    

    should be inside your init() function. Every time you call those you are adding another click handler to the div. You only want to add them once, so just put them in init().

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

Sidebar

Related Questions

fairly new to Objective-C and iOS development (coming from PHP) and I have a
I'm fairly new to Objective C, but have had some experience with Java. In
I am fairly new to Objective C and iOS programming but am constantly trying
I am fairly new to Objective-C, but haven't been able to find the answer
I am fairly new to objective c and cocoa, however i have spent a
I have a fairly simple sync problem. I have a table with about 10
I am fairly new with Objective C and getting my feet wet! I have
I'm fairly new to programming in Objective-C. While I have been able to find
I have a fairly simple website where users can fill out a form describing
I am trying to put two different UIPopovers in one view. I'm fairly new

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.