3

I am trying to using a dropdown and an input box and then submit both. For some reason, it is not

function watchForm() {
  $('.decisions').on('click', function(event) {
    event.preventDefault;
    const state = $('.js-query-states').val();
    console.log(state)
  })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="decisions">

  <select class="states js-query-states">
    <option value="">Choose a state</option>
    <option value="AL">Alabama</option>
  </select><br><br>
  <label for="number">How many</label>
  <input type="input" name="number" id="number" value=5>
  <input type="submit" value="Submit">
</form>

4
  • What exactly is not working? Why are you listening clicks on the form? Commented Apr 28, 2020 at 5:30
  • Where do you call watchForm()? Commented Apr 28, 2020 at 5:31
  • You're missing the parentheses after event.preventDefault, so you're not calling the function. Commented Apr 28, 2020 at 5:32
  • What are you trying to do with the input box? Commented Apr 28, 2020 at 5:33

3 Answers 3

1

Few issues here:

  • You need to listen to the Form .submit() event instead of click().
  • Also, you are missing the parentheses () after event.preventDefault.
  • Also, it seems that the event handler is inside the watchForm() function, but it is not called anywhere. So, you can remove that function and add the .submit() event handler directly.

$('.decisions').submit(function(event) {
  event.preventDefault();
  const state = $('.js-query-states').val();
  console.log(state)

  const number = $('#number').val();
  console.log(number)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="decisions">
  <select class="states js-query-states" required>
    <option value="">Choose a state</option>
    <option value="AL">Alabama</option>
  </select><br><br>
  <label for="number">How many</label>
  <input type="input" name="number" id="number" value=5>
  <input type="submit" value="Submit">
</form>

Sign up to request clarification or add additional context in comments.

1 Comment

thank you. I have watchForm() called but just didn't include it in the code I sent. looks like the () after the event.prevent Default() was the main issue but thanks for all the pointers
0

It should be event.preventDefault() like Barmar said. Also it's good to have submit event for the form.

Comments

0

To submit the value of the "select" DOM element you must set the "name" attribute and then, "submit" button will include it when you clicked on it

<form class = "decisions">

    <select class="states js-query-states" name="state">
        <option value= "">Choose a state</option>
        <option value= "AL">Alabama</option>
    </select><br><br>
    <label for= "number">How many</label>
    <input type = "input" name = "number" id= "number" value = 5>
    <input type="submit" value = "Submit">
</form>

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.