You will need to do two things:
Stop resetting the entire
.innerHTML
because that wipes out all objects in that sub-hierarchy and creates all new ones (losing all event handlers and styling).Dynamically create your new elements and use on of the
.append()
methods and then call.iCheck()
on them after creating in order to inherit the new functionality.
Code to do both of those using jQuery:
function add() {
var userInput = $("#reg").val();
$("<li> <input type='checkbox'> <label>"+ userInput + "</label> </li>")
.appendTo("#checklist")
.find("input")
.iCheck({
checkboxClass: 'icheckbox_polaris',
radioClass: 'iradio_polaris',
increaseArea: '-10%' // optional
});
}
Conceptually, this is what this code does:
- Get the reg value.
- Create a new HTML fragment using the reg value
- Append that to the end of the existing checklist
- Find the new input tag in that new HTML fragment
- Call
.iCheck()
on it to bless it with the desired behaviors/styles like the other ones.