0

I have this div and and button near the div:

<div id="tempData"></div>
<input type="button" text="status" disabled>

When page load the button is disabled.

At some point I add elements programmatically to div when div not empty it has to be enabld. How can I make button enabled when div not empty and disabled when it empty?

6
  • Enable it in whatever logic is adding elements to the div. Commented Oct 30, 2018 at 17:40
  • Possible duplicate of Hide button if div empty Commented Oct 30, 2018 at 17:42
  • This SO post contains some great ideas: stackoverflow.com/questions/4665466/… Commented Oct 30, 2018 at 17:43
  • With CSS: #tempData:empty + input { pointer-events: none; } (although, honestly, it's probably a bad idea). Commented Oct 30, 2018 at 17:46
  • @DavidThomas doesn't stop tab focus from getting to the element. Commented Oct 30, 2018 at 17:50

2 Answers 2

3

Pure javascript:

First check empty when your triggering action occurs, whatever that may be:

function whatevertriggeredthis(){
    if(document.getElementById("tempData").innerHTML === ""){

       // Give the button an ID then enable it
       document.getElementById("status_button").disabled = false;

    }
}

JQuery Version:

$("#tempData").on("change",function(){
   if($(this).text() !== ""){
      $("button").prop("disabled",false);
   }
});
Sign up to request clarification or add additional context in comments.

Comments

1

function yourFunctionToUpdateDiv(){

  //YOUR CODE TO ADD/REMOVE DIV CONTENT
  //...
  //...
  
  
  if ($('#tempData').html()){ 
    $("input").prop('disabled', false);
  }
  else{
    $("input").prop('disabled', true);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.