1

I'm not sure this question has the best of titles but I'm not sure what else to call it so sorry for that.

I'm using ajax to pull in content for a div on my website (after an option is selected). The content is a form generated by a PHP script. When the form is submitted a JavaScript function should be called but I'm just getting an error that says the function can't be found.

The JavaScript is pulled in via ajax with the form and I can't really change that as it needs to change demanding on the option selected.

My question is should this work? if not I'll just have to re think the way I'm doing it, just wanted to check if it wasn't working because it never will or if I'm doing something wrong.

I would show the code but it's very long.

Thanks in advance!

Edit: thanks for all the comments ect, apologies for not including the code before here it is.

function select(id){
    $.ajax({
            url: 'select/'+id,
            type: 'GET',
            dataType: 'html',
            success: function(msg) {
                $('.product_details').html(msg);
                return false;
            }
    });
}
5
  • 3
    Just show the relevant part of the code (the Ajax call, for ex.). Or use pastebin or codepad. Are you using any framework (jQuery, Dojo, Mootols...) for the Ajax part? in jQuery you can look into delegate() or on() (> 1.7) maybe, but it's difficult to tell without seeing anything Commented Nov 23, 2011 at 16:23
  • I'd presume that adding a script element via "append()" or "html()" doesn't actually execute it, meaning that included functions won't be declared. It seems strange that your code has to change based on user choices Commented Nov 23, 2011 at 16:29
  • 3
    What you want to do is possible, but we really can't help you unless we see what you're doing. Commented Nov 23, 2011 at 16:30
  • The most important thing with your problem is you have to "evaluate" the js code you are inserting into the page with ajax. The examples (at least the MooTools one) given by @TimWickstrom.com should help you with it. Commented Nov 23, 2011 at 20:31
  • Should be working like a champ, do you have a link? Commented Nov 28, 2011 at 20:22

1 Answer 1

3

Are you using a javascript library?

With jQuery specify a data type of html and make sure the script tags are before the HTML in the response

$.ajax({
    url: "something.php",
    dataType: "html",
    success: function(data, text, request) {
        ...
    }
});

in mootools...

var myRequest = new Request({
    url: "something.php",
    evalScripts: true,
    onSuccess: function(responseText, responseXML){
        ....
    }
});

myRequest.send();

Now your passed tags will be evaluated and available to the DOM

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

2 Comments

Hi TimWickstrom.com, thanks for you reply, I did try adding the dataType: "html" in but it doesn't seen to have fixed the problem.
Thanks for your help I've sorted it now!

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.