I have a page on my site which, when a button is pressed, pulls in an AJAX response in a <div>. The response also has AJAX in it (this all works as it should).

I want to be able to, after several checkboxes are checked, enable a button so that the process on the page can be completed and the workorder updated to "ready for pickup" status. (it's a workorder site and this particular page has several checkboxes on it that, as certain steps in a workflow is done, the user checks which updates the DB).

My problem is that I can't get the simple JS that I'm using to run. What it does is checks to see that ALL of the checkboxes are checked and if so, it enables the "Complete" button.

Now I understand that I can't run JS in an AJAX request directly, but that I need to eval() it or use some other method. Well here's my test (which isn't working).

On the ajax request (which is a database populated form), I've added this at the beginning:

<script type="text/javascript" id="runscript">
alert("This Works!!");
</script>

Then, on the main page which displays the ajax request (in a div), I've added at the end of the page:

<script type="text/javascript">
eval(document.getElementById("runscript").innerHTML);
</script>

(This was per the advice of an ajax expert that I know, but he wasn't specific on everything).

Well I would assume that as soon as I click the button to load the ajax request, an alert will popup saying "This Works!", but it doesn't. Once I get this to work, then I know that I can get the js that I need to use to work as well.

What am i doing wrong, and/or is there a better way to do this?

Recommended Answers

All 4 Replies

Why would you use eval() to call the function? What you could do if the script is being added to the DOM after the call, you could simply call it right after the add (and still inside the success & ready status check).

Maybe this test can help you out:

<script type='text/javascript'>
window.onload = function() {
    var strScript = 'function dynamicLoadTest(text){ alert("dynamicLoadTest: " + text);  }';
    eval(strScript);
    dynamicLoadTest("Fuck! It's works!");
}
</script>

Tried your suggestion...no go. :(

What error do you get? I tried this code on IE, FF, Opera and Chrome, and work fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.