0

I have this html code. I'm trying to show a table when the button is clicked. It works when button is outside all the divs. But I need to write the click event when button is still inside.

Here's my HTML code:

                   <div class="col-md-4 col-sm-4 col-6">
                        <div class="view overlay hm-white-slight">
                            <img src="images/doctor-new.png" class="img-fluid img-thumbnail" alt="">
                            <p><strong class="text-success">All Appointments</strong></p>
                            <button id="click" class="btn btn-success">Show</button>
                            <a >
                                <div class="mask"></div>
                            </a>
                        </div>
                    </div> 

And here's my jquery code:

        <script>
            $(document).ready(function (){
                $('#appointment-table').hide();
                $('div.col-md-4 button#click').click(function (){
                    $('#appointment-table').fadeToggle('slow');
                });
            });
        </script>

But it is not working... Can anyone please help?

5
  • $('#click') as a selector could be enough Commented Oct 12, 2017 at 16:04
  • But it's not working Sir.... That's why I use something like that (jquery selectors) Commented Oct 12, 2017 at 16:05
  • You could try $('div.col-md-4').on("click","#click",function(){... Commented Oct 12, 2017 at 16:05
  • The truth is, there is no element with the id appointment-table within the code provided. In the following JSFiddle, I have changed nothing except for adding a div with the id appointment-table: jsfiddle.net/066oodg8/1 Commented Oct 12, 2017 at 16:10
  • If you reference the button by its ID, ie $("#click") and <button id='click'/> and there is only one button with that ID (because IDs must be unique within the document) and your html is not added dynamically (ie after your doc ready) - then it will work anywhere. So if it's not working, one of the above is not correct and your question does not make it clear which one. Commented Oct 12, 2017 at 16:12

1 Answer 1

1

There is no problem here, other than the fact that there is no element in your code with the id appointment-table.

JSFiddle: https://jsfiddle.net/066oodg8/

$(document).ready(function() {
  $('#appointment-table').hide();
  $('div.col-md-4 button#click').click(function() {
    $('#appointment-table').fadeToggle('slow');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="col-md-4 col-sm-4 col-6">
  <div class="view overlay hm-white-slight">
    <img src="images/doctor-new.png" class="img-fluid img-thumbnail" alt="Thumbnail Alt">
    <p><strong class="text-success">All Appointments</strong></p>
    <button id="click" class="btn btn-success">Show</button>
    <a>
      <div class="mask"></div>
    </a>
  </div>
</div>
<div id="appointment-table">
  Appointment Table
</div>

However you may notice a flicker as the #appointment-table is hidden; To avoid this use the CSS display: none; on the element instead of hiding it with jQuery.

$(document).ready(function() {
  $('div.col-md-4 button#click').click(function() {
    $('#appointment-table').fadeToggle('slow');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="col-md-4 col-sm-4 col-6">
  <div class="view overlay hm-white-slight">
    <img src="images/doctor-new.png" class="img-fluid img-thumbnail" alt="Thumbnail Alt">
    <p><strong class="text-success">All Appointments</strong></p>
    <button id="click" class="btn btn-success">Show</button>
    <a>
      <div class="mask"></div>
    </a>
  </div>
</div>
<div id="appointment-table" style="display: none;">
  Appointment Table
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't work Sir... When the button is outside the div function works as expected. but in this case, it is not... I'm wondering why.... :(
@the_dempa Unless you are viewing this from the mobile app, there should be a button that says "Run code snippet". That should run the code I've posted. It seems to work fine for me...
Yes this is correct and works fine. I even tried adding my code to fiddle.net but in my computer it is not working unless the button is outside the div

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.