Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why won't $(".unhide-btn").prop("disabled", false) work?

When i press the hide button it is also supposed set disabled to false to the unhide button but it does nothing but hide the element.

    $(document).ready(function() {
      $(".unhide-btn").prop("disabled", true)
      $("button").click(function() {
        $("p").hide()
        $(".unhide-btn").prop("disabled", false)
        $("button").prop("disabled", true)
      })
      $(".unhide-btn").click(function() {
        $("p").show()
        $(".unhide-btn").prop("disabled", true)
        $("button").prop("disabled", false)
      })
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <body>
  <script src="script.js"></script>
  <p>Hide me!</p>
  <button>Hide</button>
  <button class="unhide-btn">Unhide</button>
</body>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As I mentioned in my comment, your click event on the button applies to all your buttons, which is causing the issue. The simple fix would instead be to add a class to your hide button (like you have for your unhide button) and select that:

$(document).ready(function() {
  $(".unhide-btn").prop("disabled", true)
  $(".hide-btn").click(function() {
    $("p").hide()
    $(".unhide-btn").prop("disabled", false)
    $(".hide-btn").prop("disabled", true)
  })
  $(".unhide-btn").click(function() {
    $("p").show()
    $(".unhide-btn").prop("disabled", true)
    $(".hide-btn").prop("disabled", false)
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<p>Hide me!</p>
<button class="hide-btn">Hide</button>
<button class="unhide-btn">Unhide</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading