Sorry for answring my question.
Thank you very much for the solution. I tried outing some code in fiddler but not able to get it work dues to some dependencies. But I will try to explain step by step the requirement and what I have done so far.
- User should be able to search an agent name in autocomplete and clicks add. Autocomplete is making an Ajax call to server to get the names and unique id’s.
- Should be allowed to add maximum 4 and minimum 1. Should be able to deleted added name(Just by mistake if he adds wrong name)
- Currently I am using appenTo function and dynamically creating input boxs with selected value 4 times. Input name = Agent + indexValue ex: Agent1, Agent2Agent3 and Agent4.
- Need to give an option to select one of those names as primary.Thought of adding a radio button next to text box.
- I have a submit button that submits form to the server. Where I am trying to see the value with alert for those dynamically generated input filed values and getting undefined.
var agt1 =$("#Agent1").val();
alert (agt1); //Undefined error.
Even if I make it work, input value is (Ex: John Dow) is not something I want to submit to server, instead id of that name. So I decided input box idea is not going to work and then trying to switch to selectable widget. But here also it seems we cannot pass ID of the selected item.
I think what I need is like creating a select box with multi select true and then add options to the select box dynamical when clicking add agent button (Track 4 times) add John Dow Matt Damn Tom Cruice Brad Pitt pass all max 4 option values to the server and indicate 25 is selected.
If you have any better option, please let me know, I would like to try. Thank You.
http://jsfiddle.net/XYr3t/
if($('#container').find('.agent').length < 4) {
var len = $('#container').find('.agent').length;
var index = len+1;
$('#container').append('<div><label >Agent' +index+ '</label><input class="agent" id ="Agent" '+ index +' name ="Agent" '+ index +' type="text" value ="'+agent+'"><span class="remove" ><div class="ui-state-default ui-corner-all"><span style="padding-left:3px" class="ui-icon ui-icon-minusthick"></span></div></span></div>');
$( '[name=selectAgent]' ).val('');
}else{
$.msgBox({
title:"Agent Name",
content:"You cannot add more than 4 Agents!"
});
}
})
$('body').on('click', '.remove', function() {
if($('#container').find('.agent').length > 0) {
$(this).parent().remove();
}
})