Reputation: 444
I have one application that multiple instances of it exists in the same page. I have one sort of slide show within the application that uses the input
to show corresponding data. However the label
and input
only works with one instance in the page. so if there are to instance of the same application within the page, only the first one works.
Here is a demo: JSFiddle
$.fn.MP = function(options) {
var settings = $.extend({
// These are the defaults
text: ""
}, options);
$(this).on("click tap", "input", function() {
$(this).parent().siblings().find("input").prop("checked", false);
});
};
$("#item1").MP({});
$("#item2").MP({});
input[type="checkbox"] {
position: absolute;
opacity: 0;
}
.panelTop > div div {
display: none;
}
.panelTop > div input[type="checkbox"]:checked ~ div {
display: block;
}
.panel {
width: 400px;
height: 100px;
border: 1px solid black;
}
.panel > .panelTop {
width: 100%;
height: 49px;
border-bottom: 1px dashed black;
}
.panel > .panelBottom {
width: 100%;
height: 50px;
}
.panel > .panelTop > div div {
width: 24px;
height: 24px;
float: right;
margin: 0px 9px 0 0;
border: 1px dashed black;
}
.panel > .panelBottom > div {
width: 32px;
height: 32px;
float: right;
margin: 9px 9px 0 0;
border: 1px solid black;
text-align: center;
font-size: 22px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel" id="item1">
<div class="panelTop">
Dependant Buttons:
<div>
<input id="box-1" type="checkbox" checked>
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</div>
<div>
<input id="box-2" type="checkbox">
<div>2.1</div>
<div>2.2</div>
<div>2.3</div>
</div>
<div>
<input id="box-3" type="checkbox">
<div>3.1</div>
<div>3.2</div>
<div>3.3</div>
</div>
</div>
<div class="panelBottom">
Main Buttons:
<label for="box-1" class="btn1">1</label>
<label for="box-2" class="btn2">2</label>
<label for="box-3" class="btn3">3</label>
</div>
</div>
<div class="panel" id="item2">
<div class="panelTop">
Dependant Buttons:
<div>
<input id="box-1" type="checkbox">
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</div>
<div>
<input id="box-2" type="checkbox">
<div>2.1</div>
<div>2.2</div>
<div>2.3</div>
</div>
<div>
<input id="box-3" type="checkbox">
<div>3.1</div>
<div>3.2</div>
<div>3.3</div>
</div>
</div>
<div class="panelBottom">
Main Buttons:
<label for="box-1" class="btn1">1</label>
<label for="box-2" class="btn2">2</label>
<label for="box-3" class="btn3">3</label>
</div>
</div>
So as you can see in the demo, there are two instances of the application, clicking on buttons of the 1, 2 or 3
makes change in the first instance even if you click on the second instance.
I want to make each instance work correctly and each radio button work only if it is pressed.
Any idea?
Upvotes: 0
Views: 84
Reputation: 2307
As @dryden-long already mentioned, IDs must be unique. Fix that, and modify the corresponding for
attributes accordingly, and your issue is resolved.
EDIT: Per your comment, since the id
values of your div.panel
elements are already unique, you could do something like the following, which dynamically generates unique id
values for your input
elements, and updates the for
values of the corresponding label
elements accordingly:
$.fn.MP = function(options) {
var settings = $.extend({
// These are the defaults
text: ""
}, options);
$(this).on("click tap", "input", function() {
$(this).parent().siblings().find("input").prop("checked", false);
});
};
// dynamically-generate id/for values
$('div.panel').each(function(index, el) {
var $panel = $(el);
var panelId = $panel.attr('id');
var $panelBottom = $('div.panelBottom', $panel);
$('div.panelTop > div > input', $panel).each(function(indexInput, elInput) {
var $input = $(elInput);
var inputId = panelId + '-box-' + indexInput;
$input.attr('id', inputId);
$('label:nth(' + indexInput + ')', $panelBottom).attr('for', inputId);
});
});
$("#item1").MP({});
$("#item2").MP({});
input[type="checkbox"] {
position: absolute;
opacity: 0;
}
.panelTop > div div {
display: none;
}
.panelTop > div input[type="checkbox"]:checked ~ div {
display: block;
}
.panel {
width: 400px;
height: 100px;
border: 1px solid black;
}
.panel > .panelTop {
width: 100%;
height: 49px;
border-bottom: 1px dashed black;
}
.panel > .panelBottom {
width: 100%;
height: 50px;
}
.panel > .panelTop > div div {
width: 24px;
height: 24px;
float: right;
margin: 0px 9px 0 0;
border: 1px dashed black;
}
.panel > .panelBottom > div {
width: 32px;
height: 32px;
float: right;
margin: 9px 9px 0 0;
border: 1px solid black;
text-align: center;
font-size: 22px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel" id="item1">
<div class="panelTop">
Dependant Buttons:
<div>
<input type="checkbox">
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</div>
<div>
<input type="checkbox">
<div>2.1</div>
<div>2.2</div>
<div>2.3</div>
</div>
<div>
<input type="checkbox">
<div>3.1</div>
<div>3.2</div>
<div>3.3</div>
</div>
</div>
<div class="panelBottom">
Main Buttons:
<label class="btn1">1</label>
<label class="btn2">2</label>
<label class="btn3">3</label>
</div>
</div>
<div class="panel" id="item2">
<div class="panelTop">
Dependant Buttons:
<div>
<input type="checkbox">
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</div>
<div>
<input type="checkbox">
<div>2.1</div>
<div>2.2</div>
<div>2.3</div>
</div>
<div>
<input type="checkbox">
<div>3.1</div>
<div>3.2</div>
<div>3.3</div>
</div>
</div>
<div class="panelBottom">
Main Buttons:
<label class="btn1">1</label>
<label class="btn2">2</label>
<label class="btn3">3</label>
</div>
</div>
Upvotes: 1