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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:28:04+00:00 2026-05-13T16:28:04+00:00

I have a javascript function that goes something like this: function doSomething(me) { var

  • 0

I have a javascript function that goes something like this:

function doSomething(me) {
        var isMeChecked = me.checked;
        for (i=0;i<=10;i++) {
            me.checked = !isMeChecked;
            alert (me.checked);
        }
    }

I assumed that isMeChecked would remain constant after the first load, but it apparently is a reference variable to me.checked, and so it changes with every iteration.
How do I force isMeChecked to get set to the value of me.checked when it’s first loaded, and not the reference of me.checked?

Alright, to make this more clear, I am editing this post to show actual code in use that is exhibiting the undesirable behavior:

Here are the javascript functions:

function CheckAll(me, gridViewId) {
    var isMeChecked = (me.checked)?true:false;
    alert('CheckAllFired_1');
    Parent = document.getElementById(gridViewId);
    for (i = 0; i < Parent.rows.length; i++) {
        alert('CheckAllFired_2_' + i.toString());
        var tr = Parent.rows[i];
        var td = tr.childNodes[0];
        var item = td.firstChild;
        if (item.id != me.id && item.type == "checkbox") {
            alert('CheckAllFired_3_' + i.toString() + '_' + me.checked + '_' + isMeChecked);
            item.checked = !isMeChecked;
            item.click();
        }
    }
}
function CheckSelectAll(me, checkBoxHeaderId, gridViewId) {
    alert('CheckSelectAllFired_1');
    if (!me.checked) {
        alert('CheckSelectAllFired_2');
        document.getElementById(checkBoxHeaderId).checked = false;
        return;
    }
    else {
        alert('CheckSelectAllFired_3');
        document.getElementById(checkBoxHeaderId).checked = true;
        Parent = document.getElementById(gridViewId);
        for (i = 0; i < Parent.rows.length; i++) {
            alert('CheckSelectAllFired_4_' + i.toString());
            var tr = Parent.rows[i];
            var td = tr.childNodes[0];
            var item = td.firstChild;
            if (item.type == "checkbox") {
                alert('CheckSelectAllFired_5_' + i.toString());
                if (!item.checked) {
                    alert('CheckSelectAllFired_6_' + i.toString());
                    document.getElementById(checkBoxHeaderId).checked = false;
                    return;
                }
            }
        }
        return;
    }
}

I have an asp.net gridview. The first column is a column of checkboxes, with a checkbox in the header as well that toggles “Select/Deselect All” of the other checkboxes in the column.
The checkbox in the header has it’s onclick event set to onclick="CheckAll(this, 'myGridViewClientID')"

All of the remaining checkboxes in the grid, have their onlick event set to onclick="CheckSelectAll(this, 'headerCheckBoxID', 'myGridViewClientID'"

Both the headerCheckBoxID and myGridViewClientID are set in the codebehind when the gridview is rendering.

The idea is that when the checkbox in the header is checked, it will set all the other checkboxes to the opposite checked status, and then fire their click event to simulate a click, and also fire their onclick actions (which involve things like changing the color of their row, setting the datakey of the row into a SelectedValues array if it is checked, etc).

However, I also wanted the child checkboxes to have the following behavior:
1) If any of them get unchecked, uncheck the “select all” checkbox.
2) If all of them get checked, check the “select all” checkbox.

Now, because the click event of the child checkboxes has the opportunity to change the checked status of the headercheckbox, when the loop returns back to the “checkall” event, the state of the headercheckbox is different than when it first started, and so it ends up only checking the first child checkbox when trying to do a select all.

Using alerts, I was able to see that when the checked state of the headercheckbox is changed in the CheckSelectAll function, that value is also changed for the “isMeChecked” value in the CheckAll function which is spawning the CheckSelectAll functions.

That looks an awful lot like a reference variable then. I don’t know how to stop it from doing that.

  • 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-13T16:28:04+00:00Added an answer on May 13, 2026 at 4:28 pm

    Think about it. assume x.checked = true. run doSomething(x).
    When the function starts, me.checked is true. Now look at your for loop:

    Pass 1: me.checked is true. so isMeChecked is true. so you set me.checked to false.

    Pass 2: me.checked is false. isMeChecked is still true, because you don’t change it inside the loop. so me.checked is again set to false.

    Pass 3: see pass 2.
    …

    When the function exits, isMeChecked is true, and me.checked is false. All you have done is set me.checked to false 10 times.
    This is not a matter of isMeChecked being a reference (because it’s not), but just an error in your logic if you were expecting different results.

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

Sidebar

Ask A Question

Stats

  • Questions 452k
  • Answers 452k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe you can use the SignedXml.LoadXml method to initialize a… May 15, 2026 at 9:03 pm
  • Editorial Team
    Editorial Team added an answer Form fields are submitted as strings so params[:notes][:private] will actually… May 15, 2026 at 9:03 pm
  • Editorial Team
    Editorial Team added an answer Try using a context to setup all your mocks, then… May 15, 2026 at 9:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.