I am trying to create several buttons that create a window, depending on which button is pressed. I do this by providing each box with its own class and passing an argument to the function to determine which field is intended for targeting. Then I assign this function to each onclick attribute button on the page (I know that I should probably add an event handler to a separate file). Here is the function:
var show = function(boxNumber){
if($(this).hasClass('shown')){
$(this).removeClass('shown');
$(this).html('Show');
$('.box'+boxNumber).fadeOut(1000);
}
else{
$(this).html('Hide');
$(this).addClass('shown');
$('.box'+boxNumber).fadeIn(1000);
}
};
html:
<span onclick="show(1)">Box 1</span>
<div class="box1"></div>
etc. for more boxes
However, this feature does not work. Firstly, this keyword does not look like button targeting, because the buttonβs html does not change when clicked. However, when I replace this keyword with a button class, it works fine.
Secondly, the function freezes in the boxes in order, but then does not hide them if I press the button again.
, ! .