Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery not working in external JavaScript

I am new to jQuery and am stuck at some strange issue. I am using jQuery's change and click methods. They are working fine when used in my HTML file in the <script> tag.

Like:

<script>
    $("select,input").change(function ()
    {
        // My code and some alerts
    });
</script>

When I copied the same in external JavaScript code without <script> and imported that in my HTML it was not at all working.

Are there any changes which are needed to use jQuery in external JavaScript code?

PS: Some other non-jQuery functions present in same external JavaScript code are successfully called from HTML.

like image 372
Ajinkya Avatar asked May 21 '11 05:05

Ajinkya


People also ask

Why is external JavaScript not working?

You should put all javascript code in a <script> tag for the browser to recognize and make it run and for the src attribute of the script tag to find your 'script. js' file, it just has to be in the same directory as the html file, you don't need to specify the entire file root.

Can you put jQuery in a JavaScript file?

Your JavaScript file ( scripts. js ) must be included below the jQuery library in the document or it will not work. Note: If you downloaded a local copy of jQuery, save it in your js/ folder and link to it at js/jquery. min.

Why jQuery is not working in server?

When jQuery scripts fail to work on your Web server, chances are the jQuery file is missing or you did not include it correctly in your HTML code. Your Web server itself will never cause jQuery to not work, because jQuery runs on your visitors' computers.


2 Answers

First off, you don't want a <script> tag in an external JavaScript file, if that's how I'm reading your post.

The trick with jQuery is that your code is set to execute immediately.

You want to wrap your script so that it loads when the document is ready, in something like:

$(document).ready(function(){
    $("select,input").change(function ()
    {
        // My code and some alerts
    })
});

And you want to make sure that your file is loaded after jQuery (otherwise the $ global will not be set).


Additions:

Here is what your HTML should look like:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="jscript/myExternalJs.js"></script>

Here is what your JavaScript code should look like (note there is no script tag inside the JavaScript file):

$(document).ready(function(){
    $("select,input").change(function ()
    {
        // My code and some alerts
    })
    // Other event handlers.
});

As far as your other script... it sort of depends on what you're doing. The most important thing is to not try to hook event listeners up to objects that don't yet exist, which is why we use document.ready.

like image 160
John Green Avatar answered Sep 20 '22 15:09

John Green


Did you make sure jquery is defined before your own jquery code?

You should also make sure the DOM is ready when dealing with jquery:

$(document).ready(function() {
    $("select,input").change(function() {
        // my code and some alerts
    });

    // more code here if needed, etc.
});
like image 31
dtbarne Avatar answered Sep 22 '22 15:09

dtbarne