Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <!--
- Follow Up Response to yahoo question. http://answers.yahoo.com/question/index?qid=20110721131750AAC7lcv
- Follow Up Question: Checkboxes again but this time disable all others when one is clicked
- -->
- <head>
- <title>Javascript Select All</title>
- <script type="text/javascript" language="Javascript">
- Object.prototype.hasClass = function(class){
- var hcN = [];
- var hcL = this.length;
- var hcI = 0;
- for(hcX=0;hcX<hcL;++hcX){
- var hcClass = this[hcX].getAttribute('class') || null;
- if(hcClass != null && hcClass.indexOf(class) !== (-1)){
- hcN[hcI++] =this[hcX];
- }
- }
- return hcN;
- }
- Object.prototype.setChecked = function(sCTF){
- var sCTF = (sCTF === undefined)? true : sCTF;
- var saL = this.length;
- for(var saI=0;saI<saL;++saI){
- if(sCTF === 'invert'){
- this[saI].checked = (this[saI].checked !== true);
- }else{
- this[saI].checked = sCTF;
- }
- }
- return this;
- }
- Object.prototype.setDisabled = function(sDTF){
- var sDTF = (sDTF === undefined)? true : sDTF;
- var saL = this.length;
- for(var saI=0;saI<saL;++saI){
- if(sDTF === 'checked-other'){
- this[saI].disabled = (this[saI].checked !== true);
- }else{
- this[saI].disabled = sDTF;
- }
- }
- return this;
- }
- Object.prototype.addEvent = function(ev, fn, capture){
- capture = capture || false;
- var aeI = this.length;
- for(var aeX=0;aeX<aeI;++aeX){
- if(this[aeX].addEventListener){
- this[aeX].addEventListener(ev, fn, capture);
- }else if (this[aeX].attachEvent){
- this[aeX].attachEvent('on'+ev, fn);
- }else{
- alert('Cannot attach Event Handler');
- }
- }
- return this;
- }
- Object.prototype.removeEvent = function(ev, fn, capture){
- capture = capture || false;
- var aeI = this.length;
- for(var aeX=0;aeX<aeI;++aeX){
- if(this[aeX].removeEventListener){
- this[aeX].removeEventListener(ev, fn, capture);
- }else if (this[aeX].detachEvent){
- this[aeX].detachEvent('on'+ev, fn);
- }else{
- alert('Cannot detach/remove Event Handler');
- }
- }
- return this;
- }
- Object.prototype.isType = function(itT){
- if(itT == undefined) { return this;}
- var itN = [];
- var itL = this.length;
- var itI = 0;
- for(itX=0;itX<itL;++itX){
- if(this[itX].getAttribute('type') == itT){
- itN[itI++] = this[itX];
- }
- }
- return itN;
- }
- function cbSelectByClass(classId,cElem){
- if(cElem.checked){
- // Get All Input Elements With The Class 'cbOption', then "check" them
- document.getElementsByTagName('input').hasClass('cbOption').setChecked();
- }else{
- // Get All Input Elements With The Class 'cbOption', then "uncheck" them
- document.getElementsByTagName('input').hasClass('cbOption').setChecked(false);
- }
- }
- function cbInvert(classId){
- // Get All Input Elements With The Class 'cbOption', then "invert check" them
- document.getElementsByTagName('input').hasClass('cbOption').setChecked('invert');
- }
- function cbCheckAllChildren(elemId){
- var elem = document.getElementById(elemId);
- if(elem.CBChecked == undefined){
- elem.CBChecked = true;
- }else{
- elem.CBChecked = (elem.CBChecked !== true);
- }
- elem.getElementsByTagName('input').setChecked(elem.CBChecked);
- }
- window.onload = function(){
- // Select the Element with the id of 'disableExample', search in that element for 'input' elements, filter out 'input' elements that do not have the type attribute set to 'checkbox'
- var cb_selection1 = document.getElementById('disableExample').getElementsByTagName('input').isType('checkbox');
- // Add Event 'click', when the elements are clicked, then run the function. 'this' referes to the element that is clicked.
- cb_selection1.addEvent('click',function(){
- if(this.checked){
- // Set all 'other' elements in the selection, to disabled, if they are not checked.
- cb_selection1.setDisabled('checked-other');
- }else{
- // Set all elements of the selection to not disabled.
- cb_selection1.setDisabled(false);
- }
- }, false);
- };
- </script>
- </head>
- <body>
- <form>
- <input type="checkbox" id="selectAll" onclick="Javascript: cbSelectByClass('cbOption',this);" /> Select All (By Class)<br />
- <input type="checkbox" id="selectInvert" onclick="Javascript: cbInvert('cbOption');" /> Invert All (By Class)<br />
- <input type="checkbox" name="option1" class="cbOption"> Option 1 <br />
- <input type="checkbox" name="option2" class="cbOption"> Option 2 <br />
- <input type="checkbox" name="option3" class="cbOption"> Option 3 <br />
- <input type="checkbox" name="option4" class="cbOption"> Option 4 <br />
- <input type="checkbox" name="option5" class="cbOption"> Option 5 <br />
- <input type="checkbox" name="option6" class="cbOption"> Option 6 <br />
- <input type="checkbox" name="option7" class="cbOption"> Option 7 <br />
- <input type="checkbox" name="option8" class="cbOption"> Option 8 <br />
- </form>
- Table 1: <a href="#" onclick="Javascript: cbCheckAllChildren('table1'); return false;">Check All Of Table 1</a> | <a href="#" onclick="Javascript: document.getElementById('table1').getElementsByTagName('input').setChecked('invert'); return false;">Invert Table 1</a>
- <table id="table1">
- <tr>
- <td><input type="checkbox" name="option1"> Option 1 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option2"> Option 2 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option3"> Option 3 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option4"> Option 4 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option5"> Option 5 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option6"> Option 6 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option7"> Option 7 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option8"> Option 8 </td>
- </tr>
- </table>
- Table 2: <a href="#" onclick="Javascript: cbCheckAllChildren('table2'); return false;">Check All Of Table 2</a> | <a href="#" onclick="Javascript: document.getElementById('table2').getElementsByTagName('input').setChecked('invert'); return false;">Invert Table 2</a>
- <table id="table2">
- <tr>
- <td><input type="checkbox" name="option1"> Option 1 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option2"> Option 2 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option3"> Option 3 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option4"> Option 4 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option5"> Option 5 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option6"> Option 6 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option7"> Option 7 </td>
- </tr>
- <tr>
- <td><input type="checkbox" name="option8"> Option 8 </td>
- </tr>
- </table>
- <h3>Disable All Other Checks when one is selected</h3>
- <form id="disableExample">
- <input type="checkbox" name="option1"> Option 1 <br />
- <input type="checkbox" name="option2"> Option 2 <br />
- <input type="checkbox" name="option3"> Option 3 <br />
- <input type="checkbox" name="option4"> Option 4 <br />
- <input type="checkbox" name="option5"> Option 5 <br />
- <input type="checkbox" name="option6"> Option 6 <br />
- <input type="checkbox" name="option7"> Option 7 <br />
- <input type="checkbox" name="option8"> Option 8 <br />
- <input type="button" value="Edit" />
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement