What I have now is almost good, but when you move the mouse along a table row, the button gets removed for some reason, which I don’t want. I only what the button to be removed if you mouseout of the whole table.
HTML: Looks like any old table with 3 columns and 12 rows + a <thead> with 3 columns.
JS:
$(document).ready(function(){
$('#editData tr').hover(function(){
$('button').remove();
$(this).children('td:last-child').append('<button id="editButton">Edit</button>');
});
$('#editData').mouseout(function(){
$('button').remove();
});
});
CSS:
#editData {
width: 500px;
margin: auto;
background: #dadce1;
margin-bottom: 50px;
box-shadow: 0 0 10px 1px #e1e1e1;
}
#editData tbody {
border: 1px solid #b8b8b8;
border-top: 0;
}
#editData thead {
border: 1px solid #b8b8b8;
border-bottom: 0;
background: rgba(255,255,255,0.7);
font-size: 20px;
}
#editData thead th {
padding: 10px;
font-weight: bold;
text-align: center;
}
#editData th:last-child, #editData tr td:last-child {
width: 40px;
}
#editData tr {
text-align: center;
height: 30px;
}
#editData td {
padding: 3px;
}
#editData tr:nth-child(even) {
background: rgba(255,255,255,0.7);
}
#editButton {
width: 40px;
height: 24px;
border-radius: 5px;
padding: 0;
}
Using the second parameter
.hover. This will attach amouseleavehandler to your rows (tr).Fiddle
Basically, just balanced your
mouseenterandmouseleavehandlers for the rows.Also if you’re interested in knowing the real issue, read about
mouseoutvsmouseleave(demo at bottom of the page).The problem is,
mouseoutbubbles up so every time it fires in any descendant of the table, it will bubble up to the table element and trigger its mouseout handler.From the
mouseoutdocs:hoveris just a shorthand that attachesmouseenterandmouseleavehandlers, you could explicitly usemouseleaveinstead ofmouseoutin your original example and it’d work as well:Fiddle
This second piece of code is what you asked for: it removes all buttons when you
mouseleavethe table, while the first is my refactored version that appends a button when youmouseenterthe row and removes it when youmouseleaveit. Both have the same visible effect.Side-note: I’ve replaced the
hoverbymouseenterin the example as.hoverwould unnecessarily attach the handler to bothmouseenterandmouseleaveevents when called with a single argument.With
.hover, themouseleavefor thetrwould remove the button and create a new one and, when you move the mouse to outside the table, the event would bubble up to the table element removing the just-created button, or if you move the mouse to another row, the handler would also be called twice (mouseleaveof old row andmouseenterof the new hover target row). Of course this is not visible as these handlers would execute in microseconds and browsers may group up repaints. You can see that it is what actually happens with someconsole.logs in this fiddle.This may not make much difference in this use-case (except performance) but in many other cases you may run into issues – many questions on SO are because questioners thought that
.hover()called with a single argument would attach solely amouseenterhandler which is not the case.ps. I aimed this answer at your event bubbling/logic problem, but you don’t have to abuse jQuery for something as simple as that. @Daryl Ginn’s answer is a very suitable solution for real-world use.
=]