11

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.

2
  • How are you linking the external js to the page? Commented May 21, 2011 at 5:30
  • Using <SCRIPT language="JavaScript" SRC="jscript/myExternalJs.js"></SCRIPT> Commented May 21, 2011 at 5:33

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes I want to remove <script> tag from HTML file.So I need to add $(document).ready(fucntion(){...}). What if I have multiple methods ? Should I add them under $(document).ready(fucntion(){...}) ?
What about other non jquery functions, should I add them under document.ready ?
You probably don't need to, no. Although it may be good practice, otherwise you're polluting the global namespace. But that's a different topic. Don't worry about it for now.
@John: One question just out of curiosity. I tried loading external js (as it is without document.ready) after body tag and it also works. But I am wondering whether it is a good practice to load js at the end of page ?
A few years ago, everybody was recommending bottom of page, but that has somewhat subsided. You can do either, but I am of the school that believes that script belongs in the head of the document.
|
3

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.
});

4 Comments

I just copied the same code as present in my HTML file to external js.And can you please tell me how to define jquery in external js ?
@Ajinkya u dont't have to define in external JS but just link jquery.*.js to your HTML. it will automatically merged.
Don't define it in your external JS, put the <script src="..."> tag for jquery in your HTML, but make sure that it is logically instantiated before the javascript you posted.
@Ravi,dtbarna: Thanks for suggestions, got it :) Added document.ready and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.