1

I'm trying to disable the submit button if I choose one of the dropdown menus that include the text "Trial" in it, but I can't seem to make it work right this time.

Javascript code:

function disableSubmit()
{
  //disable submit button
  if ($("#inputDropdown").toString().includes("Trial")) {
      $("button").attr("disabled","disabled");
  } else {
      $("button").removeAttr("disabled");
  }
}

$(function()
 {
    $(".inputDropdown").on("change keyup",disableSubmit)
})

Html form:

<form class="mb-3" id="submitform" action="page.php" method="post" accept-charset="UTF-8">
  <div class="form-group mb-4">
    <label for="name">Name</label>
    <input id="name" name="name" class="form-control" type="text" placeholder="Name">
  </div>
  <div class="form-group mb-4">
    <label for="email">Email</label>
    <input id="email" name="email" class="form-control" type="text" placeholder="Email">
  </div>
  <div class="form-group mb-4">
    <label for="name">Available Package</label>
    <select class="form-control" id="inputDropdown" name="inputDropdown">
      <option value="0" selected> - </option>
      <option value="1"> Option 1 which is Trial </option>
      <option value="2"> Here is another dropdown text </option>
      <option value="3"> #3 is the charm but nada </option>
      <option value="4"> Whatever you make Trial with it </option>
    </select>
  </div>
  <div class="col-md-2 mx-auto">
    <button class="btn btn-primary btn-block" type="submit">Add</button>
  </div>
</form>

I'm trying to disable the submit button if someone choses either #1 or #4 which have the word Trial in them.

Yamile
  • 11
  • 2
  • 1) your dropdown doesn't have `class='inputDropdown'` so your event handler never fires - change the selector to `$("#inputDropdown").on...` – freedomn-m Aug 25 '22 at 07:38
  • 2) debug every part of your code - in this case `$("#inputDropdown").toString()` will *always* give `[object Object]` - so that part is clearly wrong. See existing answer for how to get the selected text. – freedomn-m Aug 25 '22 at 07:39
  • @freedomn-m Thank you for your reply! I already checked the existing answer and fixed it! You are correct the selector was wrong changed it to `.form-group`, added `:selected` and replaced `.toString()` with `.text()` – Yamile Aug 25 '22 at 11:13

0 Answers0