-3

Probably an easy one. When I click on submit button in a form I have created I am supposed to see the message "clicked" on my console but it does not appear. Any ideas why this happens?

$("#button").on("click", function() {
  console.log("clicked");
  return false;
});
<div class="col-md-8">
  <form>
    <div class="form-group">
      <label for="name" class="sr-only">Name</label>
      <input type="text" class="form-control" id="name" placeholder="Your name here" maxlength="20">
    </div>
    <div class="form-group">
      <label for="email" class="sr-only">Email</label>
      <input type="email" class="form-control" id="email" placeholder="Your email here*" required="required">
    </div>
    <label for="textBox" class="sr-only">TextBox</label>
    <textarea class="form-control message-box" id="textBox" style="resize:none" cols="40" rows="5" placeholder="Your message here" minlength="20"></textarea>
    <button type="submit" class="btn btn-default" id="button">Submit</button>

  </form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
aptbs85
  • 101
  • 1
  • 1
  • 7

1 Answers1

0

The $ function is defined only by jQuery. You need to include jQuery for this to work.

$("#button").on("click", function() {
  console.log("clicked");
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-8">
  <form>
    <div class="form-group">
      <label for="name" class="sr-only">Name</label>
      <input type="text" class="form-control" id="name" placeholder="Your name here" maxlength="20">
    </div>
    <div class="form-group">
      <label for="email" class="sr-only">Email</label>
      <input type="email" class="form-control" id="email" placeholder="Your email here*" required="required">
    </div>
    <label for="textBox" class="sr-only">TextBox</label>
    <textarea class="form-control message-box" id="textBox" style="resize:none" cols="40" rows="5" placeholder="Your message here" minlength="20"></textarea>
    <button type="submit" class="btn btn-default" id="button">Submit</button>

  </form>

Adding jQuery works here.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252