Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checkbox add input field delete input field

    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.

    Code:
    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);
    }

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Code:
    $('#chkToggleInput').click(function(){
    	if ($(this).is(':checked')){
    		$('<input type="checkbox" id="chk1" value="yes" />').appendTo('#container');
    	}else{
    		$('#chk1').remove();
    	}
    });
    Code:
    <div id="container">
    	<input type="checkbox" id="chkToggleInput" />
    </div>

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that worked. thank you very much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •