Skip to content Skip to sidebar Skip to footer

Page Reloads On Hide/show Button Click Using Jquery

I'm using jQuery to show/hide a div by clicking on the show/hide buttons. However, my code doesn't work because every time it returns to the way it was before when I click on my bu

Solution 1:

That's because your button elements have no type specified, and by default button elements have their type set to "submit". When you click one of the buttons, they attempt to submit your form. Simply specifying a type of "button" will fix this:

<buttontype="button"class="close"id="hidemultmachines"onclick="..."></button>

Solution 2:

A couple of things here:

1) It would be best to separate the HTML from the jQuery. 2) The default behavior for a button is to submit a form, which means it will refresh the page if there is no form action. This can be fixed with preventDefault()

To sum this up in code:

HTML

<buttonclass="close"id="hidemultmachines" >Hide section below <imgalt="Close"width="35"height="35"src="../images/close.png"></button>

JS:

$(document).ready(function() {
$("#hidemultmachines").on("click", function(e) {
  e.preventDefault();
  $('#multmachines').hide();    
  $(this).hide();
  $('#showmultmachines').show();
});
});

Post a Comment for "Page Reloads On Hide/show Button Click Using Jquery"