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 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 am trying to make each number displayed clickable. 1 should alert() 80, 2
I have a NSTableView with multiple columns. clicking each of the columns sorts by
I have a page with news item titles, clicking each one ajax loads a
I have a ton of new files to commit. Clicking each one in git
Clicking on the image should show div near it (.show() in jQuery). But how
I have a page with multiples divs like this <div class=bigbox> <a href=http://www.somestuff.com>some stuff</a>
I'm using Ajax to dynamically populate a DIV. I have each page content stored
In this scenario (your basic catalog), there are multiple <div> s, each containing an
I have a list of small images while clicking on each of them they
I've three divs. Each div must increment its counter val upon clicking them. HTML:

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.