0

Due to some reasons i m dynamically creating a checkbox in a Grid..

Also i m dynamically creating a Button in the same Grid.

On this particular button click i need to check if the checkboxes are checked or not.

If the checkboxes are checked i should uncheck it.and vice versa.

Is it possible to achieve this in javascript?

3
  • 2
    Yes, it's possible. Is that the entirety of your question? Commented Jun 7, 2012 at 2:13
  • yes..................... Commented Jun 8, 2012 at 2:52
  • In other words, you didn't actually ask a question that required more than a yes/no response. If you have a specific question about how to do this, then we'll be positively glad to help if you show us what you have so far, and explain exactly where you think it's gone wrong. Commented Jun 8, 2012 at 12:17

2 Answers 2

3

Yes it is possible.

document.querySelector("button").addEventListener("click",function(){
    [].forEach.call(document.querySelectorAll('input[type="checkbox"]'),function(el){
        el.checked = !el.checked   //Invert
    });​
}, false);

No matter the checkboxes are created dynamically or not, this code will still work because it does not even need any id or class.

For IE:

document.getElementsByTagName("button")[0].addEventListener("click", function(){
    var ele = document.getElementsByTagName("input");
    for(var i=0;i<ele.length;i++){
        if(ele[i].type == "checkbox"){
            el.checked = !el.checked   //Invert
        }
    }
});

Ultimate jQuery solution!

$("button").click(function(){
    $("input[type=checkbox]").each(function(){
        this.checked = !this.checked;
    }
});

LIVE DEMO: http://jsfiddle.net/DerekL/jfeAd/

Sign up to request clarification or add additional context in comments.

6 Comments

Note that IE 8 in quirks mode and IE 7 and lower do not have support for querySelector. Also there is no requirement in any standard for navtive methods to support host objects as their this object. So calling such methods on DOM objects (like the NodeList used here) can be expected to (and do) throw errors in conforming browsers. Try it in IE 8 and lower.
Also, this function only uncheckes the checked ones, it doesn't check the unchecked ones per the OP.
Keep trying, addEventListener is not supported by IE 8 and lower. And what feature test(s) are you planning to use to chose which version of the code to run?
@RobG - I hate IE! jQuery is my final weapon.
For those who have been programming for the web for some time, IE 8 (and even 7 and 6) is a snap compared to the days of IE 4 and NN4. :-)
|
0

You can collect checkboxes within a certain context (some contianer within the grid) and set their checked property to the opposite of whatever it currently is, so:

var grid = <something that returns a reference to the grid>
var cb, cbs = grid.getElementsByTagName('input');

for (var i=0, iLen=cbs.length; i<iLen; i++) {
  cb = cbs[i];
  if (cb.type == 'checkbox') {
    cb.checked = !cb.checked;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.