0

Current code:

<form>
  <div class="form-group" id="deadline" >
    <input type="text" class="datepicker form-control" id="deadline" name="deadline" placeholder="DD/MM/YYYY" >
    <input type="checkbox" value="ongoing" name="Ongoing" id="ongoing" >
    <label for="ongoing">Ongoing</label>
  </div>
</form>

how do i make it so that checking the "Ongoing" checkbox disables the "deadline" input field? Thanks ahead of time.

3
  • Your best bet is to use javascript Commented Nov 23, 2016 at 11:16
  • could you give an example of how i would go about doing that? Commented Nov 23, 2016 at 11:18
  • @JakeB checked my answer, it is with JS and you used deadline for div as idwhich caused the issue Commented Nov 23, 2016 at 11:34

6 Answers 6

1

please use jQuery or javascript

<form>
  <div class="form-group" id="deadline" >
    <input type="text" class="datepicker form-control" id="deadline" name="deadline" placeholder="DD/MM/YYYY" >
    <input type="checkbox" value="ongoing" name="Ongoing" id="ongoing" >
    <label for="ongoing">Ongoing</label>
  </div>
</form>


<script>
 $("#ongoing).change(function(){
    if($(this).is(':checked')
   {
     $('#deadline').attr('disabled','disabled');
   }
    else{
                  $('#deadline').removeAttr('disabled');
        }
 });
<script>
Sign up to request clarification or add additional context in comments.

Comments

0

Using jQuery would be easiest way in my opinion

$('#ongoing').change(function() {
  if(this.checked) {
    $('#deadline').prop('disabled',true);
  } else {
    $('#deadline').prop('disabled',false);
  }
});

5 Comments

This is not tagged with jQuery.
The author seems to be searching for a solution. Plain HTML is not a solution. You shouldn't down vote just for that.
I have agreed to that and removed the downvote. :)
Regardless of what my tags were, it's not possible otherwise. Thjs was a quick and simple solution to the issue in the fewest lines possible, thanks
@JakeB Great. Check out my answer below for better and short code of this version. Upto you to take the best... :)
0

you can use jQuery for this...Your code will be as follow-

 $('#ongoing').change(function() {
            if($(this).is(":checked")) {
                $('#deadline').prop('disabled',false);
            }
          else
    {  $('#deadline').prop('disabled',true);} 
        });

4 Comments

This is not tagged with jQuery.
yeah so using jqury or javasript is the only option.. dont just downvote beace the question isnt tagged with jquery..it is also not tagged wiht javascript...i dont understand your thinking
@PraveenKumar your argument is baseless
Yea, agreed mate.
0

Add an onclick event and call a function like this: and remove id of this div <div class="form-group" id="deadline" > since you are using the same id for field.

<html>


<script>
var disabled=0;
function disableEnableField(){

    if(disabled==0){ //disable
     document.getElementById('deadline').disabled = true;
       disabled=1; 
     } 
    else{  //enable again
     document.getElementById('deadline').disabled = false;
      disabled=0; 
    } 
}
</script>

<form>
  <div class="form-group" >
    <input type="text" class="datepicker form-control"  id="deadline" name="deadline" placeholder="DD/MM/YYYY" >
    <input type="checkbox" value="ongoing" onclick="disableEnableField()" name="Ongoing" id="ongoing" >
    <label  >Ongoing</label>
  </div>
</form>


</html>

2 Comments

Mate, I tried this. But is this really working? Coz I couldn't make it working. See my answer if you can see it.
@PraveenKumar it is working... check my updated answer..it didn't work previously because of repeated id
0

The more better and quicker solution than the one provided is:

$('#ongoing').change(function() {
  $('#deadline').prop('disabled', this.checked);
});

Comments

0

Remove or rename that form-control div to some other name.The same id name for input tag and div is conflicting and the code is looking for the first one .Thats why here no codes working.I edited accordingly.Kindly go through it.include jquery .Its important.

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" ></script>


<script>
$(function () {
        $("input[name='Ongoing']").click(function () {
            if ($("#ongoing").is(":checked")) {
                $("#deadline").removeAttr("disabled");
                
              
                
            } else {
                $("#deadline").attr("disabled", "disabled");
                
                 
            }
        });
    });
</script>

</head>

i edited your code as required.

<body>
<form>
  <div class="form-group" id="deadlinediv" >
    <input type="text" class="datepicker form-control" id="deadline" name="deadline" placeholder="DD/MM/YYYY" disabled="disabled">
    <input type="checkbox" value="ongoing" name="Ongoing" id="ongoing" >
    <label for="ongoing">Ongoing</label>
  </div>
</form>
</body>
</html>

So many other ways already explained .May be this too will help somebody.

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.