0

I'm trying to use the JQuery .click on a div that i append, but the click doesn't work at all. here is the code, i already tried the help from the other Tutorials i could find on the internet but it didn't work.

here is the jquery (very simple version) :

$(document).ready(function() {
$( "#DivResult" ).click(function() {

    alert('yolo');
 //$( "#return" ).submit();
});
});

And the way i create my DivResult with Ajax and append :

$(document).ready(function () {
    $('#monForm').on('submit', function (e) {
        e.preventDefault();
        var $this = $(this);
        var adrall = $('#villeDepart').val();
        var adrarr = $('#villeArrivee').val();
        var datedepart = $('#datedepart').val();
        var nombreplace = $('#nombreplace').val();
        $.getJSON('/app.php/rechT/refresh', {data: adrall + ' , ' + adrarr + ' , ' + datedepart + ' , ' + nombreplace}
        ).done(function (Res) {
            console.log(Res);
            $("#resultcontainer").html("");
            if (Res == "") {
                secondchoix();
                tiercechoix();
            }
            else {
                $.each(Res, function (ligne, result) {
                    var resultats =
                            "<div class=" + '"output container col-lg-8 blueskycont col-lg-offset-2"' + ' id="DivResult"'+ 
                            ">" + " <form name=" + '"retour"' + " action=" +
                            '"/app.php/validationdemande/go"' + "role=" + '"form"' + " method=" + '"POST"' + " class=" + '"retour"' + ' id="return"'+
                            "> <div class=" + '"form-group col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + ">" +
                            "<label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12 move"' + " for=" + '"pwababd"' + ">" + result.TRAJ_LIEU_DEP + " -> " + result.TRAJ_LIEU_ARR + "</label>" +
                            " <label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + "> Départ le : " + result.TRAJ_DATE + "</label> " +
                            " <label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + "> De : " + result.PROT_LIEU_DEP + "</label> " + "</br>" +
                            " <label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + "> " + result.PROT_LIEU_ARR + "</label> " + "</br>" +
                            " <label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + "> A : " + result.TRAJ_NB_PLACE + " places restante(s) </label> " + "</br>" +
                            "<label class=" + '"funzies col-lg-2 col-md-2 col-xs-sm-2 col-xs-2"' + "> " + result.PROF_PRENOM + " " + result.PROF_NOM + " </label> " +
                            "<input type=" + '"HIDDEN" class="" id="" name="PROT_ID" value="' + result.PROT_ID + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="PROT_RED" value="' + result.PROT_RED + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="TRAJ_TYPE" value="' + result.TRAJ_TYPE + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="VILLE_DEP" value="' + result.TRAJ_LIEU_DEP + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="VILLE_ARR" value="' + result.TRAJ_LIEU_ARR + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="LIEU_DEP" value="' + result.PROT_LIEU_DEP + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="LIEU_ARR" value="' + result.PROT_LIEU_ARR + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="nombreplace" value="' + nombreplace + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="demande_ville_dep" value="' + adrall + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="demande_ville_arr" value="' + adrarr + '">' +
                            "</div>  </form> </div>";

                    $("#resultcontainer").append(
                            resultats
                            );
                });
            }
        })
                .fail(function (Res) {
                    alert('fail');          
                })
                .error(function (Res) {
                    alert('erreur');
                });
    });
});

I'm trying to use it to submit since : onClick="document.forms[\'retour\'].submit();" doesn't work on IE.

But the clic does not respond at all on the DivResult. Does it mean i can't use a Jquery.click on a div that I append?

1
  • Actually, .click() simulate a click. Use .on("click", function() { // Whatever you want }); or .onMouseDown(function() { // Whatever you want });, but I prefere the first one. Commented Jun 4, 2015 at 13:46

2 Answers 2

2

You need event delegation!

$("#resultcontainer").on("click", "#DivResult", function() {

});

Handlers are bound at runtime - and since your elements don't exist then, no handlers are bound. This will check #resultcontainer each click for the #DivResult and run the handler if found.

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

1 Comment

Thanks a lot i would never have find it out myself... it works very well, even on IE! Thank you very much :)
0

Try adding the yolo part inside of the bottom of your big results function. My theory is that your DivResult doesn't exist yet when you are calling 'onclick':

$(document).ready(function () {
    $('#monForm').on('submit', function (e) {
        e.preventDefault();
        var $this = $(this);
        var adrall = $('#villeDepart').val();
        var adrarr = $('#villeArrivee').val();
        var datedepart = $('#datedepart').val();
        var nombreplace = $('#nombreplace').val();
        $.getJSON('/app.php/rechT/refresh', {data: adrall + ' , ' + adrarr + ' , ' + datedepart + ' , ' + nombreplace}
        ).done(function (Res) {
            console.log(Res);
            $("#resultcontainer").html("");
            if (Res == "") {
                secondchoix();
                tiercechoix();
            }
            else {
                $.each(Res, function (ligne, result) {
                    var resultats =
                            "<div class=" + '"output container col-lg-8 blueskycont col-lg-offset-2"' + ' id="DivResult"'+ 
                            ">" + " <form name=" + '"retour"' + " action=" +
                            '"/app.php/validationdemande/go"' + "role=" + '"form"' + " method=" + '"POST"' + " class=" + '"retour"' + ' id="return"'+
                            "> <div class=" + '"form-group col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + ">" +
                            "<label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12 move"' + " for=" + '"pwababd"' + ">" + result.TRAJ_LIEU_DEP + " -> " + result.TRAJ_LIEU_ARR + "</label>" +
                            " <label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + "> Départ le : " + result.TRAJ_DATE + "</label> " +
                            " <label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + "> De : " + result.PROT_LIEU_DEP + "</label> " + "</br>" +
                            " <label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + "> " + result.PROT_LIEU_ARR + "</label> " + "</br>" +
                            " <label class=" + '"funzies col-lg-12 col-md-12 col-xs-sm-12 col-xs-12"' + "> A : " + result.TRAJ_NB_PLACE + " places restante(s) </label> " + "</br>" +
                            "<label class=" + '"funzies col-lg-2 col-md-2 col-xs-sm-2 col-xs-2"' + "> " + result.PROF_PRENOM + " " + result.PROF_NOM + " </label> " +
                            "<input type=" + '"HIDDEN" class="" id="" name="PROT_ID" value="' + result.PROT_ID + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="PROT_RED" value="' + result.PROT_RED + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="TRAJ_TYPE" value="' + result.TRAJ_TYPE + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="VILLE_DEP" value="' + result.TRAJ_LIEU_DEP + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="VILLE_ARR" value="' + result.TRAJ_LIEU_ARR + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="LIEU_DEP" value="' + result.PROT_LIEU_DEP + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="LIEU_ARR" value="' + result.PROT_LIEU_ARR + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="nombreplace" value="' + nombreplace + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="demande_ville_dep" value="' + adrall + '">' +
                            "<input type=" + '"HIDDEN" class="" id="" name="demande_ville_arr" value="' + adrarr + '">' +
                            "</div>  </form> </div>";

                    $("#resultcontainer").append(
                            resultats
                            );
                });
            }
        })
                .fail(function (Res) {
                    alert('fail');          
                })
                .error(function (Res) {
                    alert('erreur');
                });
    });

    $( "#DivResult" ).click(function() {
        alert('yolo');
 //$( "#return" ).submit();
    });
});

1 Comment

I'm not sure if i understood very well your point but i suppose it was existing since the other answer worked, by delegation of my event. I guess i wasn't using the good listener

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.