Ищите только другие кнопки в родительском элементе:
var box = $(".Button");
box.click(function() {
var $this = $(this);
$this.toggleClass("Green");
$this.parent().find(".Button").not(this).removeClass("Green");
});
Пример:
var box = $(".Button");
box.click(function() {
var $this = $(this);
$this.toggleClass("Green");
$this.parent().find(".Button").not(this).removeClass("Green");
});
.Button {
font-weight: bold;
font-size: 13px;
background: #0096db;
text-align: center;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
padding: 15px;
color: #fff;
position: static;
float: right;
min-width: 25%;
max-width: 40%;
}
.Button.Green {
background: #64B448;
font-weight: bold;
font-size: 13px;
text-align: center;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
padding: 15px;
color: #fff;
position: static;
float: right;
min-width: 25%;
max-width: 40%;
}
.productset1 {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 2fr));
grid-template-rows: repeat(auto-fit, minmax(10px, 1fr));
grid-gap: 5px;
}
.productset2 {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 2fr));
grid-template-rows: repeat(auto-fit, minmax(10px, 1fr));
grid-gap: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Product Set 1</h1>
<div class="productset1">
<span class="Button">Button 1</span>
<span class="Button">Button 2</span>
</div>
<h1>Product Set 2</h1>
<div class="productset2">
<span class="Button">Button 3</span>
<span class="Button">Button 4</span>
</div>
Или, как Дэвид Томас указал , получите его без сценария с радиовходами:
Пример:
.Button {
font-weight: bold;
font-size: 13px;
background: #0096db;
text-align: center;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
padding: 15px;
color: #fff;
position: static;
float: right;
min-width: 25%;
max-width: 40%;
}
input[type=radio] {
display: none;
}
input[type=radio]:checked + .Button {
background: #64B448;
font-weight: bold;
font-size: 13px;
text-align: center;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
padding: 15px;
color: #fff;
position: static;
float: right;
min-width: 25%;
max-width: 40%;
}
.productset1 {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 2fr));
grid-template-rows: repeat(auto-fit, minmax(10px, 1fr));
grid-gap: 5px;
}
.productset2 {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 2fr));
grid-template-rows: repeat(auto-fit, minmax(10px, 1fr));
grid-gap: 5px;
}
<h1>Product Set 1</h1>
<div class="productset1">
<input id="btn1" type="radio" name="set1">
<label for="btn1" class="Button">Button 1</label>
<input id="btn2" type="radio" name="set1">
<label for="btn2" class="Button">Button 2</label>
</div>
<h1>Product Set 2</h1>
<div class="productset2">
<input id="btn3" type="radio" name="set2">
<label for="btn3" class="Button">Button 3</label>
<input id="btn4" type="radio" name="set2">
<label for="btn4" class="Button">Button 4</label>
</div>
... но это увеличивает сложность разметки.: -)