Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Execute jQuery before Javascript with oncall

How can one execute the jQuery before this Javascript? Currently, I am getting in the logs 2, 1 when I would like to get 1, 2.

<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>

<form method="POST" id="post-form">
   <input type="submit" onclick="createDiv();" name="Send" >
</form>

<script>
 $(document).ready(
      $('#post-form').submit(function(e){
  console.log(1);
      })
    );
</script>

<script>
 function  createDiv() {
   console.log(2);
 }
</script>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As you know first click button event is triggering then form is submitting. Therefore you output is logically correct. But still if wanna change the flow you can do some modification to achieve that.
To ensure that the jQuery code executes before the JavaScript code, you can modify your script as follows:

<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>

<script>
  $(document).ready(function() {

  function createDiv() {
    console.log(2);
  }

    $('#post-form').submit(function(e) {
      console.log(1);
      createDiv();
    });
  });
</script>

<form method="POST" id="post-form">
  <input type="submit" name="Send">
</form>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading