jQuery append if doesn't exist

前端 未结 3 2087
你的背包
你的背包 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:27

    In order to check if an element exists, you need to find a selector that matches only that element and check if the length of the jQuery object is equal to 0 or not, like so:

    if ($('#unique-selector').length === 0) {
        // code to run if it isn't there
    }
    else {
        // code to run if it is there
    }
    

提交回复
热议问题