2

I am setting up a navbar that contains a button that is expected to submit the form in the body (album_form). Any suggestions on what to put in for the onclick to accomplish this?

<body>
  <header>
    <navbar>
      <button type="submit" name="save_album" class="btn" id="save_album" onclick="????" value="Save Album">Save Album</button>
    </navbar>
  </header>
  <main>
    <form action="/album/" method="POST" name="album_form" id="album_form">
     <input />
    </form>
  </main>
</body>
Barkermn01
  • 6,781
  • 33
  • 83
bldev
  • 137
  • 1
  • 1
  • 12
  • Possible duplicate of [How to submit a form with JavaScript by clicking a link?](http://stackoverflow.com/questions/6799533/how-to-submit-a-form-with-javascript-by-clicking-a-link) – Tyler Roper Dec 16 '16 at 15:39

3 Answers3

2

You dont need anything in the onclick. Just add this javascript.

document.getElementById("save_album").addEventListener("click", function() {
  document.getElementById("album_form").submit();
});

DEMO https://jsfiddle.net/4xcmh2ms/6/

Brad
  • 8,044
  • 10
  • 39
  • 50
0

already there are questions and answers related to this topic, see this one: Submit form using a button outside the tag!

Hope this help you.

Or you can write a little javascript code:

<form id="Form">
  <!-- form fields and/or controls-->
</form>
<input type="submit" id="externalButton">

<script>
$(document).ready(function () {
  $("#externalButton").click(function () {
    $("#Form").submit();
  });
});
</script>
Community
  • 1
  • 1
Beto
  • 11
  • 4
0

You can try following way:

<button type="submit" name="save_album" class="btn" id="save_album"     onclick="document.getElementById('album_form').submit()" value="Save Album">Save Album</button>
Hanif
  • 3,739
  • 1
  • 12
  • 18