1

I have buttons that display a form when clicked. I'm currently trying to figure out how to disable the other buttons once one button is already clicked, to prevent multiple forms from showing. I'm not good at javascript so I appreciate any help I may receive.

For example here are are 2 of my buttons that will display a form.

<button type="button" class="fbutton" id="formButton">1</button>
<button type="button" class="fbutton" id="formButton2">2</button>

Here is the javascript function to to display the forms, when the button is clicked.

$("#formButton").click(function () {
$("#form1").toggle();
});

$("#formButton2").click(function () {
$("#form2").toggle();
});
j08691
  • 204,283
  • 31
  • 260
  • 272
BB956
  • 31
  • 3
  • 11

7 Answers7

0

you can try this disable button after submit But the type of your button is button. So, change to $('form').submit(function() { $(this).find("button[type='button']").prop('disabled',true); });

Duc Nguyen
  • 530
  • 8
  • 16
0

You can try This

 <button type="button" onclick="document.getElementById('formButton').disabled=true;document.getElementById('formButton2').disabled=false;" class="fbutton" id="formButton">1</button>
<button type="button" onclick="this.disabled=true;document.getElementById('formButton').disabled=false;" class="fbutton" id="formButton2">2</button>
Harsha
  • 176
  • 1
  • 16
0

You could try something like this:

$("#formButton").click(function () {
if($('#formButton2').prop("disabled")) {
    $('#formButton2').attr("disabled", false);
  } else {
    $('#formButton2').attr("disabled", true);
  }
$("#form1").toggle();
});

$("#formButton2").click(function () {
if($('#formButton').prop("disabled")) {
    $('#formButton').attr("disabled", false);
  } else {
    $('#formButton').attr("disabled", true);
  }
$("#form2").toggle();
});

This will disable the opposite button upon clicking, and re-enable it upon clicking again.

Samsimus12
  • 28
  • 5
0

Something like this should work

$("#formButton").click(function() {
  $("#form1").toggle();
  $("#formButton2").prop('disabled', !$("#formButton2").prop("disabled"))
});

$("#formButton2").click(function() {
  $("#form2").toggle();
  $("#formButton").prop('disabled', !$("#formButton").prop("disabled"))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button type="button" class="fbutton" id="formButton">1</button>
<button type="button" class="fbutton" id="formButton2">2</button>

<form id="form1" style="display:none">
  <h1>Form 1</h1>
</form>

<form id="form2" style="display:none">
  <h1>Form 2</h1>
</form>
0

In this kind of cases, I often suggest the use of the .data() method.

I sligthly changed the HTML markup in order to add a data-* attribute.

So on click, the script retreives the value needed to complete the "target" id of the element to show.

About the disabling of any other button, the .not(this) filters out the actual clicked button from the collection to disable.

$(document).ready(function(){
  $(".fbutton").on("click",function(){
    var target = $(this).data("formid");
    console.log(target);
    
    $(".fbutton").not(this).prop("disabled",true);
    $("#form"+target).show();
  });
});
form[id^='form']{
  display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

<button type="button" class="fbutton" data-formid="1">1</button>
<button type="button" class="fbutton" data-formid="2">2</button>

<form id="form1">Form #1</form>
<form id="form2">Form #2</form>

Notice that the above script will work for as many button/form pairs as you wish without the need to add more lines.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
0

You can disable all buttons except active one. You can add more functionalities inside button click event.

$('#formButton').off().on('click', function () { 
   $(".fbutton").each(function () {
       $(this).prop("disabled", true);
   });
    $(this).prop("disabled", false);
});

$('#formButton2').off().on('click', function () { 
   $(".fbutton").each(function () {
        $(this).prop("disabled", true);
   });
   $(this).prop("disabled", false);
});
Pritam Jyoti Ray
  • 320
  • 1
  • 8
  • 12
0
<!DOCTYPE html>
<html>
<body>
<button
    onclick="this.disabled=false;document.getElementById('down7913').disabled=true;"
    type="submit"
    class="positive"
    name="up7913"
    id="up7913"
  >
    First
  </button>

  <button
    onclick="this.disabled=false;document.getElementById('up7913').disabled=true;"
    type="submit"
    class="negative"
    name="down7913"
    id="down7913"
  >
    Second
  </button>

enter code here

</body>
</html>