Is there any way to check if an element has jquery select2 already applied to it?

后端 未结 4 2073
遇见更好的自我
遇见更好的自我 2021-01-03 18:03

I want to apply select2 to a bunch of jquery elements on the page that all have the same class name but it looks like if i call select2() on an element that already has had

4条回答
  •  误落风尘
    2021-01-03 18:14

    Above answer is almost correct.
    But it creates problem when we are adding elements dynamically on same page and applying select 2 to newly created element.
    At that times selector has to be specified using not only class but also with input type. PFB reference code.

    $('inputp[type="text"].MyDripdowns').each(curr_idx, curr_elem){
        //Check if select 2 is already applied or not
        if($(curr_elem).hasClass('.select2-offscreen')){
          //Select 2 is already applied to this element
        }
        else{
           //Apply Select 2 to this element 
        }
    }
    

提交回复
热议问题