I’m trying to display the values of different checkboxes in a div and then beside the values there is an X which when clicked deletes the value from the list. So far I’ve managed to do all this, however when the user clicks on the X I can’t get the checkbox related to the value to uncheck?
You could use the checkbox indexes to create a 1:1 relationship with the X which will work regardless but if the DOM changes before them this will of course break the index numbers so maybe wrap them in a DIV which will allow them to retain a 0-5 index range and so on, see the following jsFiddle for an example using your existing code.
I’ve just got one more problem with this - when 5 checkboxes are selected the remaining one is disabled, but when you click the X it remains disabled (even though there is less then 5 checked)? Any suggestions?
And change var totalChecked = $(‘input[type=checkbox][class=mainlist]:checked’).length >=5
in: var totalChecked = $(‘input[type=checkbox][class=mainlist]:checked’).length >=6 ?
I’ve been attempting to implement the new code which you have provided, but I’m having a few problems. I understand that you mentioned wrap them in a DIV to allow them to retain a 0-5 index range. However, I don’t think it would be possible with my current project. Each individual checkbox is wrapped in a DIV and then put into an unordered list. But of course when I do this the code no longer works…
Sorry to be asking for help again but I can’t figure this one out.
I’ve made up a small example of how the checkboxes should be positioned and what is currently happening here
In that case you could use data attributes to assign a reference to the checkboxes, see the following link for how you would implement this. Essentially it’s an attribute such as data-ref=“1” that you set on the checkbox then you can use jQuery to get the value by using the data() method which I’ve already been using in the previous examples.