felix001 felix001 - 1 month ago 14
HTML Question

Add/Remove Inputs from Jquery Array in HTM Form

I need to add and remove inputs from a form. And then add remove the input values from an array that I can eventually POST.

The input additions and removes are fine. But I cannot get the array to print the new values(?)

HTML

<div id="p_scents">
<p>
<label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="NETWORK/[IP/CIDR]" /></label><a href="#" id="addScnt">Add Another Input Box</a>
</p>
<p class="result"></p>
</div>


JQUERY

$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
var encdom_local = ['something'];

$('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" name="p_scnt_' + i +'" value="" placeholder="NETWORK/[IP/CIDR]" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
encdom_local.push(1);
i++;
return false;
});
$('#remScnt').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});

$(".result").html(encdom_local.join(""));
});

Answer

If what you mean by print is this line of your code:

$(".result").html(encdom_local.join(""));

Then it 'happens' only once, after you add your event listeners. If u want to print it everytime, just add this printing method to the .live() events.

Comments