admin管理员组文章数量:1323336
I'm trying to add bootstrap toggle switch dynamically but it does not display well after adding it. Thanks for your help!
<input id="TheCheckBox" type="checkbox" data-off-text="Yes" data-on-text="No" checked="false" class="BSswitch">
<p> <a class="btn btn-lg btn-primary" href="#">Display another Toggle</a></p>
$('.btn').on('click', function () {
$('p').after(
'<input id="TheCheckBox" type="checkbox" data-off-text="Male" data-on-text="Female" checked="false" class="BSswitch">'
);
});
This is the Jsfiddle: /
I'm trying to add bootstrap toggle switch dynamically but it does not display well after adding it. Thanks for your help!
<input id="TheCheckBox" type="checkbox" data-off-text="Yes" data-on-text="No" checked="false" class="BSswitch">
<p> <a class="btn btn-lg btn-primary" href="#">Display another Toggle</a></p>
$('.btn').on('click', function () {
$('p').after(
'<input id="TheCheckBox" type="checkbox" data-off-text="Male" data-on-text="Female" checked="false" class="BSswitch">'
);
});
This is the Jsfiddle: http://jsfiddle/icamilo95/8wars2ep/3/
Share Improve this question edited Jun 23, 2015 at 3:46 m4n0 32.4k28 gold badges80 silver badges96 bronze badges asked Jun 22, 2015 at 10:32 Camilo OrdonezCamilo Ordonez 1392 silver badges11 bronze badges 2- What is not displayed? I can see the toggle switches here. – m4n0 Commented Jun 22, 2015 at 10:34
- Thanks Manoj. It only displays a regular checkbox, not the bootstrap toggle once you add it dynamically with JQuery. You can check the Jsfiddle – Camilo Ordonez Commented Jun 23, 2015 at 0:37
3 Answers
Reset to default 4You need to call the bootstrapSwitch() again after adding and with a new ID, class or else it will alter the states of existing toggle as well.
$('.btn').on('click', function () {
$('p').after(
'<input id="newCheckBox" type="checkbox" data-off-text="Male" data-on-text="Female" checked="false" class="newBSswitch">'
);
$('.newBSswitch').bootstrapSwitch('state', true); // Add
});
JSfiddle
I was facing the same issue using bootstrap-toggle. Then after debugging, I understood the behaviour and handled it.
If the checkbox is not in the dom when page loads, then we need to initialize the toggler when the checkbox is available to the dom and also any listener methods.
if you are doing an ajax call then in the success method you can add the following in the end.
$("[data-toggle='toggle']").bootstrapToggle('destroy')
$("[data-toggle='toggle']").bootstrapToggle();
sometimes it may take time to add to the dom and your code gets run before checkbox getting added to the dom, you can wrap the above code in a javascript timer
setTimeout( () => {
$("[data-toggle='toggle']").bootstrapToggle('destroy')
$("[data-toggle='toggle']").bootstrapToggle();
$(".checkbox").change(function() {
console.log("toggled");
});
}, 100 );
Add Bootstrap toggle js after your main js file
Then call bootstrapswitch statement in your js file
$('.your_toggle_class_name').bootstrapSwitch('state', true);
本文标签: javascriptBootstrap toggle doesn39t display well when added dynamicallyStack Overflow
版权声明:本文标题:javascript - Bootstrap toggle doesn't display well when added dynamically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742136705a2422402.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论