$(document).ready(function() {
var nr = 0;
$("#raspuns").text(nr);
$("#t1 #e1").on({
mouseover: function() {
$(this).css("background-color", "yellow");
},
mouseleave: function() {
$(this).css("background-color", "green");
},
click: function() {
$(this).off('mouseleave');
$("#t1 #e1").off('mouseleave');
$("#t1 #e2").off('mouseleave');
$("#t1 #e3").off('mouseleave');
$("#t1 #e4").off('mouseleave');
$(this).off('mouseover');
$("#t1 #e1").off('mouseover');
$("#t1 #e2").off('mouseover');
$("#t1 #e3").off('mouseover');
$("#t1 #e5").off('mouseover');
$(this).off('click');
$("#t1 #e4").off('click');
$("#t1 #e2").off('click');
$("#t1 #e3").off('click');
$("#t1 #e5").off('click');
nr = 1;
$("#raspuns").text(nr);
}
});
$("#t1 #e2").on({
mouseover: function() {
$(this).css("background-color", "yellow");
$("#t1 #e1").css("background-color", "yellow");
},
mouseleave: function() {
$(this).css("background-color", "green");
$("#t1 #e1").css("background-color", "green");
},
click: function() {
$(this).off('mouseleave');
$("#t1 #e1").off('mouseleave');
$("#t1 #e5").off('mouseleave');
$("#t1 #e3").off('mouseleave');
$("#t1 #e4").off('mouseleave');
$(this).off('mouseover');
$("#t1 #e1").off('mouseover');
$("#t1 #e5").off('mouseover');
$("#t1 #e3").off('mouseover');
$("#t1 #e4").off('mouseover');
$(this).off('click');
$("#t1 #e4").off('click');
$("#t1 #e1").off('click');
$("#t1 #e3").off('click');
$("#t1 #e5").off('click');
nr = 2;
$("#raspuns").text(nr);
}
});
$("#t1 #e3").on({
mouseover: function() {
$(this).css("background-color", "yellow");
$("#t1 #e1").css("background-color", "yellow");
$("#t1 #e2").css("background-color", "yellow");
},
mouseleave: function() {
$(this).css("background-color", "green");
$("#t1 #e1").css("background-color", "green");
$("#t1 #e2").css("background-color", "green");
},
click: function() {
$(this).off('mouseleave');
$("#t1 #e1").off('mouseleave');
$("#t1 #e2").off('mouseleave');
$("#t1 #e5").off('mouseleave');
$("#t1 #e4").off('mouseleave');
$(this).off('mouseover');
$("#t1 #e1").off('mouseover');
$("#t1 #e2").off('mouseover');
$("#t1 #e5").off('mouseover');
$("#t1 #e4").off('mouseover');
$(this).off('click');
$("#t1 #e4").off('click');
$("#t1 #e2").off('click');
$("#t1 #e1").off('click');
$("#t1 #e5").off('click');
nr = 3;
$("#raspuns").text(nr);
}
});
$("#t1 #e4").on({
mouseover: function() {
$(this).css("background-color", "yellow");
$("#t1 #e1").css("background-color", "yellow");
$("#t1 #e2").css("background-color", "yellow");
$("#t1 #e3").css("background-color", "yellow");
},
mouseleave: function() {
$(this).css("background-color", "green");
$("#t1 #e1").css("background-color", "green");
$("#t1 #e2").css("background-color", "green");
$("#t1 #e3").css("background-color", "green");
},
click: function() {
$(this).off('mouseleave');
$("#t1 #e1").off('mouseleave');
$("#t1 #e2").off('mouseleave');
$("#t1 #e3").off('mouseleave');
$("#t1 #e5").off('mouseleave');
$(this).off('mouseover');
$("#t1 #e1").off('mouseover');
$("#t1 #e2").off('mouseover');
$("#t1 #e3").off('mouseover');
$("#t1 #e5").off('mouseover');
$(this).off('click');
$("#t1 #e1").off('click');
$("#t1 #e2").off('click');
$("#t1 #e3").off('click');
$("#t1 #e5").off('click');
nr = 4;
$("#raspuns").text(nr);
}
});
$("#t1 #e5").on({
mouseover: function() {
$(this).css("background-color", "yellow");
$("#t1 #e1").css("background-color", "yellow");
$("#t1 #e2").css("background-color", "yellow");
$("#t1 #e3").css("background-color", "yellow");
$("#t1 #e4").css("background-color", "yellow");
},
mouseleave: function() {
$(this).css("background-color", "green");
$("#t1 #e1").css("background-color", "green");
$("#t1 #e2").css("background-color", "green");
$("#t1 #e3").css("background-color", "green");
$("#t1 #e4").css("background-color", "green");
},
click: function() {
$(this).off('mouseleave');
$("#t1 #e1").off('mouseleave');
$("#t1 #e2").off('mouseleave');
$("#t1 #e3").off('mouseleave');
$("#t1 #e4").off('mouseleave');
$(this).off('mouseover');
$("#t1 #e1").off('mouseover');
$("#t1 #e2").off('mouseover');
$("#t1 #e3").off('mouseover');
$("#t1 #e4").off('mouseover');
$(this).off('click');
$("#t1 #e1").off('click');
$("#t1 #e2").off('click');
$("#t1 #e3").off('click');
$("#t1 #e5").off('click');
nr = 5;
$("#raspuns").text(nr);
}
});
});
div {
height: 50px;
width: 200px;
background: green;
}
table {
border-color: black;
border-collapse: collapse;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script>
</script>
</head>
<body style="background-color : #90C3D4">
<table id="t1" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>
<div id="e1"></div>
</td>
</tr>
<tr>
<td>
<div id="e2"></div>
</td>
</tr>
<tr>
<td>
<div id="e3"></div>
</td>
</tr>
<tr>
<td>
<div id="e4"></div>
</td>
</tr>
<tr>
<td>
<div id="e5"></div>
</td>
</tr>
</table>
<p id="raspuns"></p>
</body>
</html>
Hello guys, I did a selector that can select some parts of the table, now I want to make a function that makes 5 table with a random number of lines (ex : 5 tables with 4 lines),and if we selected from third table some elements then tables 1 and 2 automatically selects and so on, something like this
give me some ideas or some code. Thank You.
$("#e1, #e2, #e3, #e4").off('mouseleave');does the job of the four separate lines of code you currently have to takemouseleaveoff those elements? (And the rest of your long function could be similarly simplified for the other events. Actually with appropriate classes you wouldn't need separate.on()handlers either, but I don't have time to explain that now.)