32

Please i need a script that can work with the HTML code bellow to disable/enable the button when a checkbox is checked or unchecked,

<input type="submit" name="sendNewSms" class="inputButton" id="sendNewSms" value=" Send " />

please pals i don't mean the button to be disabled when checked, but rather the other way round.

Destiny
  • 479
  • 1
  • 6
  • 13

6 Answers6

61

You can use onchangeevent of the checkbox to enable/disable button based on checked value

<input type="submit" name="sendNewSms" class="inputButton" id="sendNewSms" value=" Send " />

<input type="checkbox"  onchange="document.getElementById('sendNewSms').disabled = !this.checked;" />
Yuriy Galanter
  • 36,794
  • 12
  • 65
  • 122
21

HTML

<input type="checkbox" id="checkme"/><input type="submit" name="sendNewSms" class="inputButton" id="sendNewSms" value=" Send " />

JS

var checker = document.getElementById('checkme');
var sendbtn = document.getElementById('sendNewSms');
checker.onchange = function() {
  sendbtn.disabled = !!this.checked;
};

DEMO

brbcoding
  • 12,810
  • 2
  • 35
  • 51
  • 1
    Obviously just switch the `sendbtn.disabled = false` to `sendbtn.disabled = true` or vice versa as needed. If you want it disabled on first visit, you can just add disabled to the button to begin with. – brbcoding Aug 07 '13 at 18:49
  • thanks pal, it works fine now, thanks for the help. – Destiny Aug 07 '13 at 19:07
  • Keep in mind, you'll need to check this serverside as well. All it takes is removing `disabled` from [this line](http://i.imgur.com/m6n4vUl.png) in devtools and the button is enabled again. – brbcoding Aug 07 '13 at 19:13
  • can we do the script inline with tag? – A-letubby Dec 15 '16 at 08:39
19

You will have to use javascript, or the JQuery framework to do that. her is an example using Jquery

   $('#toggle').click(function () {
        //check if checkbox is checked
        if ($(this).is(':checked')) {

            $('#sendNewSms').removeAttr('disabled'); //enable input

        } else {
            $('#sendNewSms').attr('disabled', true); //disable input
        }
    });

DEMO: http://jsfiddle.net/T6hvz/

srakl
  • 2,475
  • 2
  • 19
  • 32
11

brbcoding have been able to help me with the appropriate coding i needed, here is it

HTML

<input type="checkbox" id="checkme"/>
  <input type="submit" name="sendNewSms" class="inputButton" disabled="disabled" id="sendNewSms" value=" Send " />

Javascript

 var checker = document.getElementById('checkme');
 var sendbtn = document.getElementById('sendNewSms');
 // when unchecked or checked, run the function
 checker.onchange = function(){
if(this.checked){
    sendbtn.disabled = false;
} else {
    sendbtn.disabled = true;
}

}
Destiny
  • 479
  • 1
  • 6
  • 13
3

Here is a clean way to disable and enable submit button:

<input type="submit" name="sendNewSms" class="inputButton" id="sendNewSms" value=" Send " />
<input type="checkbox" id="disableBtn" />

var submit = document.getElementById('sendNewSms'),
    checkbox = document.getElementById('disableBtn'),
    disableSubmit = function(e) {
        submit.disabled = this.checked
    };

checkbox.addEventListener('change', disableSubmit);

Here is a fiddle of it in action: http://jsfiddle.net/sYNj7/

johnferrie
  • 242
  • 1
  • 3
2

I recommend using jQuery as it will do all the heavy lifting for you. The code is fairly trivial.

$('input:checkbox').click(function () {
  if ($(this).is(':checked')) {
    $('#sendNewSms').click(function () {
      return false;
    });
  } else {
    $('#sendNewSms').unbind('click');
  }
});

The trick is to override the 'click' event and effectively disable it. You can also follow it up with some CSS magic to make it look "disabled". Here is the code in JavaScript in case you need it. It's not perfect but it gets the point across.

var clickEvent = function () {
  return false;
};
document.getElementById('#checkbox').onclick(function () {
  if (document.getElementById('#checkbox').checked) {
    document
      .getElementById('#sendNewSms')
      .onclick(clickEvent);
  } else {
    document
      .getElementById('#sendNewSms')
      .removeEventListener('click', clickEvent, false);
  }
});
brbcoding
  • 12,810
  • 2
  • 35
  • 51
beautifulcoder
  • 9,954
  • 3
  • 18
  • 27