0

Ajax returns all expected html result from php code, except the javacript and jQuery inside the "script" tags

 var frm = $('#cell');
    frm.submit(function (ev) {
        $.ajax({
            type: "POST",
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (html) {

    //Create jQuery object from the response HTML.
    var $return=$(html);
    //Query the jQuery object for the values
    var oneval = $return.filter('#cellWidthSlider').html();
    alert(oneval);

                $("#cellWidthDiv").html(oneval);

            }
        });

        ev.preventDefault();
    });

I have tried to append to head, but the jQuery code would execute only - before the elements.

var $response=$(html);

$("head").append($response);

var oneval = $response.filter('#cellWidthSlider').html();
SergeDirect
  • 2,019
  • 15
  • 20
  • 1
    `"but the jQuery code would execute only - after the elements."` Please explain what you mean, this question is not clear – Steve Nov 06 '14 at 11:21
  • I am echoing slider with jquery in my php script that is being processed, inside , there is some values being extracted from database which is why the – SergeDirect Nov 06 '14 at 11:25
  • sorry, I've typed after ...should have been before... – SergeDirect Nov 06 '14 at 11:27
  • 2
    Why you don't use some values from database sent as json? And after ajax callback you can call your slider or do anything else with received values – Panoptik Nov 06 '14 at 11:38
  • Panoptik, could you give an example. I need 3 values passed to js code, how would I do it with json? – SergeDirect Nov 06 '14 at 11:45
  • Your answer probably is here [http://stackoverflow.com/questions/889967/jquery-load-call-doesnt-execute-javascript-in-loaded-html-file][1] [1]: http://stackoverflow.com/questions/889967/jquery-load-call-doesnt-execute-javascript-in-loaded-html-file – Federico Nieri Nov 06 '14 at 12:02
  • I don't see how those questions are related - I need to load my script after html – SergeDirect Nov 06 '14 at 12:08

1 Answers1

0

As much as I've been afraid of JSON, it was actually much simpler than I thought and it really took me only around 3 hours to get the general understanding, feeling good, now so so much started to look familiar in ajax and jQuery:)

So The changes I've done... Instead of generating js within external php I have created array with values e.g.:

$result = array(
                array(
                       'prevCellId' => $prevCellId,
                       'prevCellWidth' => $prevCellWidth,
                       'thisCellId' => $latestCellId,
                       'thisWidth'=>$thisWidth,
                       'thisRowId' => $thisRowId,
                       'thisRowOrder' => $thisRowOrder,
                       'maxWidth' => $maxWidth,
                       'prevCellRowOrder' => $prevCellRowOrder));

echo json_encode($result); 

Then, after long search I've managed to implement json values in my jQuery function:

    $.getJSON( "cellWidth.php", function( json ) {
                   $.each(json, function(i, item) {
                   $('#slider').slider({ max:''+item.maxWidth+'',min: 10,step: 5, value:''+item.thisWidth+'', slide: handleSliderChange,   stop: sendNew});
                   var $slider = $("#slider").slider({ max:''+item.maxWidth+'',min: 10,step: 5, value:''+item.thisWidth+''});
                   })
 })

So simply used keyword "item." + "names of arrays" to "echo" / "use values" within jQuery - in this case this is a jQuery slider :))

SergeDirect
  • 2,019
  • 15
  • 20