jQuery append if doesn't exist

前端 未结 3 2093
你的背包
你的背包 2020-12-20 16:48

I would like to append a div only if it\'s not already there. I am trying with this but it doesn\'t work:

$(\'#method_id\').on(\'change\', funct         


        
3条回答
  •  难免孤独
    2020-12-20 17:32

    As you are appending div with id, you could just search if that div exists by calling :

    if($("#warning_message").length > 0 ){
       $('.form-group').find('#column_message>.alert').remove();
    }else{
       $('#column_message').append('
    Warning! Installed column and method do not match
    '); }

    But if you would like to have more divs of alert, you could just search for class "alert" or so.

    Another way without id ( probably better ) would be to use children() method

提交回复
热议问题