Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating jQuery from HTML

I have a PHP page with various forms I've been using jQuery to submit/verify etc.

The problem is my JavaScript is getting really long. Is there a way to separate them?

The jQuery is just this sort of thing:

<script>
$(document).ready(function()
{  
   $("#form_email").live("submit", function(event)
   { 
      error = "0";
      $("#warning").hide();   
      if ($('#email_new').val() == "")
      {
          error = "1";
          $("#warning").html("Don't leave blank");
          $("#warning").slideDown('slow');           
      }

      if (error == "0")
      {
          $.post("php/settings_email.php",
          {
              email_new:$('#email_new').val(),
              email_password:$('#email_password').val()
          },
          function(data)
          {$("#email_alert").html("Email address changed");});
      }
    });
});        
</script>

I've tried referencing it like this:

<script type="text/javascript" src="js/settings.js"></script>

Where am I going wrong?

.

ANSWER:

Make sure you reference your script AFTER the jQuery!

Also remove the script tags.

like image 657
penpen Avatar asked Dec 06 '25 10:12

penpen


2 Answers

This should work:

<script type="text/javascript" src="js/settings.js"></script>

If not works for you:

  • double check file path
  • check jquery is loaded before settings.js
  • check you haven't leave any script tag in your settings.js

As a suggestion, start by trying to access file by hand (writting your settings.js URL directly to the address bar). If it doesn't work, you have a file path problem or you can have a problem with file permissions (if you are using a Linux system).

like image 53
Ivan Avatar answered Dec 07 '25 22:12

Ivan


Remove the <script> tags from the .js file.

like image 26
James Montagne Avatar answered Dec 07 '25 23:12

James Montagne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!