0

I hope I can explain this properly. I am creating a JQuery dialogue box where the content is loaded from an HTML file. This dialogue box contains a form with which I need to perform additional JS functions. The problem I am running into, is that none of the HTML/selectors exist in the DOM until I click #image_edit so my JQuery upload script is not working.
As you can see, I am invoking the upload function on the .load callback, but it doesn't work. Is there a better/proper way to do this?

EDIT : After moving the .dialog and remote functions inside the load() callback, everything is working as expected.

uploadInterface.js

(function($) {
    $(function(//removed, was not needed//) {
    //upload functions//
    });
})(window.jQuery);

uploadSetup.js

(function($) {
    $(function(//removed, was not needed//) {
    //upload functions//
    });
})(window.jQuery);

dialogue.js

$(function(){
    $('#image_edit').click(function() {
        $(".upload_div").load('../dialogues/content.html #upload_window', function() {

            $.getScript("js/uploadInterface.js"); //added remote script here//
            $.getScript("js/uploadSetup.js"); //added remote script here//

        }).fadeTo("slow", 1).dialog({
            title: "Upload an image",
            width: "320px",
            position: { my: 'top', at: 'top+150' },
            resizable: false,
            draggable: false,
            modal: true,
            show: "fadeIn",
            hide: "fadeOut",
            create: function(event, ui) {
                var widget = $(this).dialog("widget");
                $(".ui-dialog-titlebar-close span", widget)
                .removeClass("ui-icon-closethick")
                .addClass("ui-icon-close-custom");
            }
        });
    });
});
6
  • What would go inside the uploadSetup function? Why can't you have regular functions with selectors and events. Even if the elements do not exist, the functions will work once they are in the DOM later after the load Commented Oct 9, 2014 at 23:00
  • I want to keep them separate for manageability, and so I can use them separately throughout the site. Commented Oct 9, 2014 at 23:13
  • so are these scripts being loaded in separate js files? Commented Oct 9, 2014 at 23:17
  • That is correct, I will edit the question Commented Oct 9, 2014 at 23:20
  • and those scripts are being included in the html file that you load? with script tag, right? Commented Oct 9, 2014 at 23:28

2 Answers 2

1

To load scripts in jQuery you should use $.getScript() So in your load callback function you would have a $.getScript() to get the script then execute whatever functions you want.

$(".upload_div").load('../dialogues/content.html #upload_window', function() {
    $.getScript( "uploadInterface.js" )
})...
...
...
Sign up to request clarification or add additional context in comments.

Comments

1

You have not provided a load() callback.

Walk through the closing braces in your code and you will see that there is only one argument passed to load() and that you are passing a second argument to dialog().

The load() method ends as soon as your chain starts .fadeTo()

This second argument of dialog is what you have misinterpreted as the callback for load()

6 Comments

I see what you're saying.
I fixed that part, but the dialogue script still doesn't recognize the upload functions.
Seems to be a scope issue?
if upload script #1 is your function declaration it's all wrong. Not sure what's going on there.
I clarified the question.
|

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.