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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:17:50+00:00 2026-05-12T23:17:50+00:00

When clicking on each div it should alert ‘1’ if div 1 was clicked

  • 0

When clicking on each div it should alert ‘1’ if div 1 was clicked on or ‘5’ if div 2 was clicked on. I have tried to make this code as easy to as possible because this is needed in a much larger application.

<html>
<head>
<style type="text/css">
#div1 { background-color: #00ff00; margin: 10px; padding: 10px; }
#div2 { background-color: #0000ff; margin: 10px; padding: 10px; }
</style>
<script type="text/javascript">

function init()
{
  var total = 1;

  var div1 = document.getElementById('div1'),
      div2 = document.getElementById('div2');

  var helper = function(event, id)
  {
      if (event.stopPropagation) event.stopPropagation();
      if (event.preventDefault) event.preventDefault();

      alert('id='+id);
  }

  div1.addEventListener('click', function(event) { helper(event, total); }, false);

  total += 4;

  div2.addEventListener('click', function(event) { helper(event, total); }, false);

}

</script>
</head>

<body onload="init();">

<div id="div1">1</div>
<div id="div2">2</div>

</body>
</html>

Thanks for your help! 🙂

  • 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-12T23:17:50+00:00Added an answer on May 12, 2026 at 11:17 pm

    The problem is that the event listeners and ‘total’ both exist in the same scope (init())

    The event functions are always going to reference total within the init() scope, even if it is changed after the event functions are declared

    To get around this, the event functions need to have a ‘total’ in their own scope which will not change. You can add another layer of scope using an anonymous function

    For example:

    (function (total) {
        div1.addEventListener('click', function(event) { helper(event, total); }, false);
    }(total));
    
    total += 4;
    
    (function (total) {
      div2.addEventListener('click', function(event) { helper(event, total); }, false);
    }(total));
    

    The anonymous functions are passed init()’s current ‘total’ value as a parameter. This sets another ‘total’ to the anonymous function’s scope, so it does not matter if init()’s total changes or not, because the event function will FIRST reference the anonymous function’s scope.

    Edit:

    Also, you need to place a semicolon after the closing brace of the helper function, otherwise the script will complain that ‘event’ is undefined.

    var helper = function(event, id)
    {
      if (event.stopPropagation) event.stopPropagation();
      if (event.preventDefault) event.preventDefault();
    
      alert('id='+id);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using Eclipse with each project in a separate window by right clicking
What tools are useful for automating clicking through a windows form application? Is this
First of all many thanks for clicking into my question. I have a few
I have a lot of buttons and by clicking on different button, different image
Clicking through to the download-page if see that the last version of the download
When clicking on the text box part of an <input type=file /> in FireFox3,
I need to find whether the user clicking the browser back button or Refresh
i need to check whether the user clicking the browser Refresh button and redirect
When I run my Visual Studio Windows Forms application by clicking F5 (debug mode),
I'm looking for the equivalent of right clicking on the drive in windows and

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.