2

I have the following inline code that I would like to move to my js file ...

$("input.checkbox").live('click',function(doStuff){my code})

I have tried various ways but no luck, my function just does nothing when I move it to my .js file. When it's inline in works perfectly. I am quite new at all of this so be gentle.

5 Answers 5

1

Did you remember to put wrap the code in a ready call inside the js file?

$(document).ready(function(){
    $("input.checkbox").live('click',function(doStuff){my code});
});

or the shortened version

$(function(){
    $("input.checkbox").live('click',function(doStuff){my code});
});
Sign up to request clarification or add additional context in comments.

1 Comment

Ok this may be a stupid question, but how to I reference the function in my XSL once I have moved it to my js file. I have tried giving it a function name ... For example in the XSL file I have the following ...
0

1- Move your code to your new JS file as you did.

2- When referencing your JS file inside your page, reference it AFTER the jQuery file, JavaScript will be executed according to the order of the files included.

<html>
    <head>
        <script type="text/javascript" src="YOUR_jQUERY_FILE"></script>
        <script type="text/javascript" src="YOUR_CUSTOM_FILE"></script>
    </head>
    <body>
        ..
        ..
        ..  
    </body>

</html>

3- Try your page now, it should be working fine.

let me know if this didn't fix it, or if it did :)

Comments

0

You need your reference to the jquery library:

Create your javascript include file myfile.js and then a reference to this file:

Obviously then copy your code into you .js and all should be dandy...

Comments

0
  1. Make sure the js file is added to the page(script tags). It is recommended to add the file to the end of the body.

  2. Make sure jQuery is added to the page before your external js file.

  3. Wrap your code inside $(document).ready(function(){[Your code here]}), so that it executes after the DOM is loaded.(this ensures your elements are available for selection)

1 Comment

Thanks for your ideas, I will make sure I am following all of your suggestions.
0

You should add your custom JS file script tag at the end of your html just before closing the tag, so all controls have been rendered. Should work fine!

2 Comments

Hi, this works perfectly. Thanks. Now I need to move this code out of my xsl file and into it's own js file. I tried adding <script type="text/JavaScript">function myfunc()</script>. And then in my js file I defined function myfunc(){ my code} but now it no longer works. Very frustrating.
You don't have to put the function reference in the calling file. Just the script tag in the calling file does the trick! If I understood the problem correctly!

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.