Select element in jQuery
html
jQuery
<style>
.cube {
height: 100px;
width: 100px;
display: inline-block;
}
.sel {
border: solid 2px blue;
}
</style>
<div id="cubes">
<div class="cube" style="background: red;"></div>
<div class="cube" style="background: yellow;"></div>
<div class="cube" style="background: red;"></div>
<div class="cube sel" style="background: green;"></div>
<div class="cube" style="background: green;"></div>
<div class="cube" style="background: red;"></div>
<div class="cube" style="background: yellow;"></div>
</div>
<button onclick="next()">next</button>
<button onclick="nextAll()">nextAll</button>
<button onclick="prev()">prev</button>
<button onclick="prevAll()">prevAll</button>
<button onclick="first()">first</button>
<button onclick="last()">last</button>
<button onclick="parent()">parent</button>
<button onclick="childrenF()">children</button>
<button onclick="filterRed()">filter red</button>
<button onclick="filterGreen()">filter green</button>
<button onclick="filterYellow()">filter yellow</button>
<button onclick="notRed()">not red</button>
<button onclick="notGreen()">not green</button>
<button onclick="notYellow()">not yellow</button>
<script>
function next() {
$(".sel").removeClass("sel").next().addClass("sel");
}
function nextAll() {
$(".sel").removeClass("sel").nextAll().addClass("sel");
}
function prev() {
$(".sel").removeClass("sel").prev().addClass("sel");
}
function prevAll() {
$(".sel").removeClass("sel").prevAll().addClass("sel");
}
function first() {
$(".sel").removeClass("sel");
$("#cubes").children().first().addClass("sel");
}
function last() {
$(".sel").removeClass("sel");
$("#cubes").children().last().addClass("sel");
}
function parent() {
$(".sel").removeClass("sel").parent().addClass("sel");
}
function childrenF() {
$(".sel").removeClass("sel").children().addClass("sel");
}
function filterRed() {
$(".sel").removeClass("sel");
$("#cubes").children().filter(function (index) {
return this.style["background-color"] == "red";
}).addClass("sel");
}
function filterGreen() {
$(".sel").removeClass("sel");
$("#cubes").children().filter(function (index) {
return this.style["background-color"] == "green";
}).addClass("sel");
}
function filterYellow() {
$(".sel").removeClass("sel");
$("#cubes").children().filter(function (index) {
return this.style["background-color"] == "yellow";
}).addClass("sel");
}
function notRed() {
$(".sel").removeClass("sel");
$("#cubes").children().not(function (index) {
return this.style["background-color"] == "red";
}).addClass("sel");
}
function notGreen() {
$(".sel").removeClass("sel");
$("#cubes").children().not(function (index) {
return this.style["background-color"] == "green";
}).addClass("sel");
}
function notYellow() {
$(".sel").removeClass("sel");
$("#cubes").children().not(function (index) {
return this.style["background-color"] == "yellow";
}).addClass("sel");
}
</script>