this is a problem that I keep coming across in my beginnings of the JS development. The problem outline is the following. We have N number of buttons on the page. We want to create an event listener for each of these buttons. Suppose the buttons' names are: "overview-tab", "return-tab" and "analysis-tab". We also have div with class names that correspond to the button names. For example, <div class="overview-tab"></div>. Initially, only one div is visible. Suppose it is the overview-tab. The goal is to display: none all of the divs, but the one that the user clicked the button on. I always start with the following, but then realise that inside of the second each, I do not have access to the name of the button from the above each:
$(":button").each(function(index, buttonElement) {
$(element).on('click', function(err) {
if(err) throw err;
$(".tabs").each(function(index, tabElement) {
// set all of the tabs, but the one clicked on (buttonElement)
// to display: none.
});
});
});
HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<input type="button" name="overview-tab" value="Overview Tab">
<input type="button" name="return-tab" value="Return Tab">
<input type="button" name="analysis-tab" value="Analysis Tab">
<div class="overview-tab" style="background:red; width: 10px; height: 10px;"></div>
<div class="return-tab" style="display: none; background: green; width: 10px; height: 10px;"></div>
<div class="analysis-tab" style="display: none; background: yellow; width: 10px; height: 10px;"></div>
</body>
</html>