I need to click an individual element within a set of elements, and select both that and an element from a different set but with the same index. each set usually consists of 2 to 6 indexes in the array (below example has 3: [ 0, 1, 2 ]), and both sets (the form radio buttons and the data itself) always have the matching number of indexes.
For example: When index 1 is clicked in the form, only the data bearing index 1 should be visible. All elements in the data set that are not clicked should hide since only 1 should show at a time. If index 1 is clicked, I should see [ 5g, 7g, 3g ] (the second index of each row). if index 0 is clicked, i should see [ 54mg, 76mg, 15mg ] (the 1st index of each row).
Clicking the data itself should not have any effect.
Here is what I tried:
<div>
<form id="form" method="get" onsubmit="return false"></form>
<label for="foo_0" set="foo" state="hidden">Option 1</label>
<input form="form" name="fooItem" id="foo_0" index="0" type="radio" set="foo" style="display: none;" />
<label for="foo_1" set="foo" state="visible">Option 2</label>
<input form="form" name="fooItem" id="foo_1" index="1" type="radio" set="foo" style="display: none;" />
<label for="foo_2" set="foo" state="hidden">Option 3</label>
<input form="form" name="fooItem" id="foo_2" index="2" type="radio" set="foo" style="display: none;" />
</div>
<br />
<br />
<div class="row">
<div class="rowName">Title 1</div>
<div class="rowData">
<div set="foo" index="0" state="hidden">54mg</div>
<div set="foo" index="1" state="visible">5g</div>
<div set="foo" index="2" state="hidden">123g</div>
</div>
</div>
<div class="row">
<div class="rowName">Title 2</div>
<div class="rowData">
<div set="foo" index="0" state="hidden">76mg</div>
<div set="foo" index="1" state="visible">7g</div>
<div set="foo" index="2" state="hidden">105g</div>
</div>
</div>
<div class="row">
<div class="rowName">Title 3</div>
<div class="rowData">
<div set="foo" index="0" state="hidden">15mg</div>
<div set="foo" index="1" state="visible">3g</div>
<div set="foo" index="2" state="hidden">87g</div>
</div>
</div>
label[state=visible] {
border: 1px solid #333333;
}
label[state=hidden] {
border: 1px solid #cccccc;
}
.row {
width: 100%;
margin-top: 6px;
margin-bottom: 4px;
padding-bottom: 2px;
border-bottom: 1px solid #cccccc;
overflow: auto;
}
.row > .rowName {
display: inline-block;
}
.row > .rowData {
display: inline-block;
float: right;
}
.row > .rowData > div[state=visible] {
display: inline-block;
}
.row > .rowData > div[state=hidden] {
display: none;
}
.row > .rowData > div[state=visible] {
display: inline-block;
width: 60px;
text-align: right;
}
.row > .rowData > div[state=hidden] {
display: none;
}
The $(this) works fine, but the 2 lines below it do not.
$(document).ready(function() {
$('label[set="foo"]').on("click", function() {
$(this).attr("state", "visible");
$(this).siblings().attr("state", "hidden");
$('.row > .rowData > div[set="foo"]').attr("state", "visible");
$('.row > .rowData > div[set="foo"]').siblings().attr("state", "hidden");
});
});
How do i select the ".row -> .rowData -> div" based on what index was clicked in "label"?