...

View Full Version : Other Checkbox add input field delete input field



stalwil
07-22-2010, 11:56 PM
Hey Guys,

I'm trying to add an input field once a checkbox is checked and then delete that same input field when the same checkbox is unchecked. I have the code that does this but when i uncheck it the second time my input field doesnt get removed it just stays there and when i check the checkbox another input field gets added. Can someone help me? Please see my code below.



function add_input(){
if ($('input[name=foo]').is(':checked')) {
document.getElementById('addInput').innerHTML += "<input id='yes' name='chk1' value=''/>";
}
else{
removeItem(this);
}
}
function removeItem(){
var d = document.getElementById('addInput');
d.removeChild(yes);
}

Spudhead
07-23-2010, 10:03 AM
$('#chkToggleInput').click(function(){
if ($(this).is(':checked')){
$('<input type="checkbox" id="chk1" value="yes" />').appendTo('#container');
}else{
$('#chk1').remove();
}
});


<div id="container">
<input type="checkbox" id="chkToggleInput" />
</div>

stalwil
07-24-2010, 01:08 AM
that worked. thank you very much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum