0

After an Ajax request, I want that the div with the ID disappeard but it doesn't work. There is no error in the console log. I've checked the condition after the ajax request with an alert() and it works so this is the remove() function wich doesn't work.

My div (it contains a lot of content like divs)

<div class="div_avion" <?php echo ($demandePHP==true)?'id="avion_idDIV:'.$dataPHP['avion_id'].'"':'';?> <?php echo ($demandePHP==false)?'style="display:none;"':'';?>>...</div>

JS

$(document).ready(function(){
        $(document).on('click', '.button-trash', function() {
            if($(this).is(".avion_bdd"))
            {
                var avion_id = $(this).attr('id').replace("avion_id:", "");
                var result = confirm("Etes-vous sûr de vouloir supprimer cet avion ?");
                if (result) {
                    $.ajax({
                       url : '<?php echo 'http://'.$_SERVER[HTTP_HOST].'/scripts/deleteavionflottehub.php';?>',
                       type : 'POST', // Le type de la requête HTTP, ici devenu POST
                       data : 'avion_id='+avion_id,
                       dataType : 'html',
                       success : function(resultat, statut){
                        if(resultat=='OK')
                        {
                            $('#avion_idDIV:'+avion_id).remove();

                        }

                       },

                       error : function(resultat, statut, erreur){

                       },

                       complete : function(resultat, statut){

                       }
                    });
                }

            }else{
                $(this).parent().parent().parent().parent().parent().remove(); 
            }
        });
});

Have you an idea?

Rob
  • 14,746
  • 28
  • 47
  • 65
Louis D.
  • 454
  • 6
  • 19
  • 2
    The colon in your `id` is interpreted as a pseudo class/selector and therefore nothing is found. Here's the solution: http://stackoverflow.com/questions/7434215/jquery-selecting-an-id-with-a-colon-in-it – Joe Aug 16 '15 at 16:20
  • 1
    Is this `$(this).parent().parent().parent().parent().parent()` suppose to be getting the `div_avion` element? An easier way to do this would be to do `$(this).closest('.div_avion')` .. But I am not really clear as to what you are trying to remove. Can you add the relevant HTML? – khollenbeck Aug 16 '15 at 16:21
  • @Joe Indeed it has solved my problem. thx – Louis D. Aug 16 '15 at 16:42
  • @KrisHollenbeck It was not my question but I've tried your suggestion and it's more simple so thx. – Louis D. Aug 16 '15 at 16:44

1 Answers1

1

In jQuery selector-syntax the : has a special meaning, see here for some examples: https://api.jquery.com/category/selectors/basic-filter-selectors/ . In your example it seems to be part of an ID-string: '#avion_idDIV:'+avion_id. You may have to look into some other way of forming your ID-strings.

Sorry! just noticed, Joe has already mentioned this point 10 mins ago! I did not mean to regurgitate his post. It is Joe who deserves the credits for this answer.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43