0

I am appending several buttons into an html span tag every time I type on different inputs.

<span id="pill_filters>
  <button id="filterCreated">Filter name here</button>
  <button id="filterCreated2">Filter name here</button>
</span>

I also wanna show a label whenever there are buttons inside of this span tag and if they aren't, I wanna hide said label.

<label id="label_sc">Search Criteria:</label>

So far my jquery is

function showSCLabel(){
  if ($("#pill_filters").html.is(':empty')){
     $("#label_sc").addClass("d-none");
  }else{
   $("#label_sc").removeClass("d-none");
  }
}

But it doesnt seem to work. The label already has "d-none" class since the beginning and even with that, it is still showing. What am I doing wrong? is this not how the :empty state works? what can I use instead? I'll appreciate a lot your help!

Falkirkh
  • 21
  • 5
  • How do you use `showSCLabel` ? Can you create a [mcve]? – Roko C. Buljan Jun 22 '20 at 22:48
  • Does this answer your question? [How do I check if an HTML element is empty using jQuery?](https://stackoverflow.com/questions/6813227/how-do-i-check-if-an-html-element-is-empty-using-jquery) – StackSlave Jun 22 '20 at 22:56

2 Answers2

0
  • if statement is missing ()
  • .html.is is invalid jQuery

Use:

if ( $("#pill_filters").is(':empty') ) {    
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

Answer without jQuery:

//span
const span=document.getElementById("pill_filters");
//label
const label=document.getElementById("label-sc");

span.addEventListener('DOMSubtreeModified',function(){
  //if innerHTML is not ""
  if(span.innerHTML){
    //show label
    label.style.display="block";
  }else{
    //hide label
    label.style.display="none";
  };
};
luek baja
  • 1,475
  • 8
  • 20