I have a web application which replaces content. This content has jquery ui check buttons. When I replace the content if a button already exists then don’t add it again:
if(!$('label[for=checkWeekM]').hasClass('ui-button'))
$('.checkWeek').button();
If I push the button (its state is checked) and if I replace the content, the button starts locked until the same content is replaced again.
I use Backbone.js to replace the content
How can I unlock the check button?
You are duplicating
idattributes and that leads to bad HTML, bad HTML leads to frustration, frustration leads to anger, etc.You have this in your template that you have hidden inside a
<div>:Then you insert that same HTML into your
.content-central. Now you have two elements in your page with the sameidattribute and two<label>elements pointing to them. When you add the jQuery-UI button wrapper, you end up with a slightly modified version of your<label>as the visible element for your checkbox; but, that<label>will be associated with two DOM elements through theforattribute and everything falls apart.The solution is to stop using a
<div>to store your templates. If you use a<script>instead, the browser won’t parse the content as HTML and you won’t have duplicateidattributes. Something like this:and then this to access the HTML:
Demo: http://jsfiddle.net/ambiguous/qffsm/
There are two quick lessons here:
<div>s, store them in<script>s with atypeattribute other thantext/htmlso that browser won’t try to interpret them as HTML.