// click handler to set the current "selected" element
document.querySelector("#gallery").addEventListener("click", function(e) {
// list items are the only valid "click" source to work with
if (e.target.nodeName !== "LI") {
return;
}
var currentlySelectedElement = e.currentTarget // the <ul> element
.querySelector(".selected");
if (currentlySelectedElement !== null) {
currentlySelectedElement.className = "";
}
e.target // the <li> element
.className = "selected";
}, false);
// click handler for the "arrow" button
var right = (function() { // Closure to only query the DOM for the <li> elements once
var items = document.querySelectorAll("#gallery li");
function getSelectedItem() {
var l = items.length,
i = 0;
for (; i < l; i++) {
if (items[i].className === "selected") {
return items[i];
}
}
return null;
}
// actual click handler to select the next element
return function() {
var selectedItem = getSelectedItem(),
nextItem;
if (selectedItem !== null) {
nextItem = selectedItem.nextElementSibling || items[0];
selectedItem.className = "";
nextItem.className = "selected";
}
};
}());
document.querySelector("#rightArw").addEventListener("click", right, false);
.selected {
outline: solid 1px red
}
#rightArw {
width: 50px;
height: 50px;
background-color: yellow
}
<ul id="gallery">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div id="rightArw"></div>
Edit
You've mentioned in the comments, that the script won't work if you add <img />
in the <li />
elements.
<li><img src="images/one.jpg" /></li>
That's because the "click" event isn't longer triggered from the <li />
but from the <img />
element. That is, the "selected" state is now set on <img />
because of e.target
.
When you now click on the "arrow" the handler looks for an <li />
with the class selected
(selectedItem = getSelectedItem()
) which he cannot find and therefor won't go to the next <li />
element (if (selectedItem !== null)
).
To get this script back to work you will have to adjust the e.target
part.
In this case you will have to walk one step up (.parentNode
) in the DOM:
document.querySelector("#gallery").addEventListener("click", function(e) {
// images are the only valid "click" source to work with
if (e.target.nodeName !== "IMG") {
return;
}
var currentlySelectedElement = e.currentTarget // the <ul> element
.querySelector(".selected");
if (currentlySelectedElement !== null) {
currentlySelectedElement.className = "";
}
e.target // the <img />
.parentNode // the <li> element
.className = "selected";
}, false);
var right = (function() {
var items = document.querySelectorAll("#gallery li");
function getSelectedItem() {
var l = items.length,
i = 0;
for (; i < l; i++) {
if (items[i].className === "selected") {
return items[i];
}
}
return null;
}
return function() {
var selectedItem = getSelectedItem(),
nextItem;
if (selectedItem !== null) {
nextItem = selectedItem.nextElementSibling || items[0];
selectedItem.className = "";
nextItem.className = "selected";
}
};
}());
document.querySelector("#rightArw").addEventListener("click", right, false);
.selected {
outline: solid 1px red
}
#rightArw {
width: 50px;
height: 50px;
background-color: yellow
}
<ul id="gallery">
<li><img src="images/one.jpg" /></li>
<li><img src="images/one.jpg" /></li>
<li><img src="images/one.jpg" /></li>
<li><img src="images/one.jpg" /></li>
</ul>
<div id="rightArw"></div>
Edit 2
I've added yet another check in the <ul />
click handler (right at the top) to prevent the handler to run on clicks which are not triggered by a <li />
(in the first example) or an <img />
in the second example.