 <p>This works:</p> <pre><code>$('input[type="radio"]').click(function() { var this_index_limit = parseInt($(this).val()); for(var i = 0; i &lt; this_index_limit; i++) { if(!$('#text_box_' + i).length) { $('&lt;input id="text_box_' + i + '" type="text"/&gt;').appendTo($(this).parent()); } else if($('#text_box_' + i).is(':hidden')) { $('#text_box_' + i).show(); } } $('input[type="text"]').each(function() { var split_id = $(this).attr('id').split('_'); if(!split_id.length) return; //from each function, not the outer click function. var index = parseInt(split_id[2]); if(index &gt;= this_index_limit) { $(this).hide(); } }); }); </code></pre> <p><a href="http://jsfiddle.net/XrZhC/2/" rel="nofollow"><h2>jsFiddle example</h2></a></p>