append selected values to the hidden form variable using jquery

I have a hidden form variable which consists of selected ID's. When i click on the icon  I launch the jquery ui diaolog make my selection and i need to append the selected ID to the hidden variable. if I do this $("#" + val2).val(ID); then each time I select a new person the old will be overwritten , but i want to retain all  selected values.

Please advise
LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?
 
leakim971PluritechnicianCommented:
so no leading semi column...
var current_persons = $("#" + val2).val(); 
current_persons = current_persons.split(";");  // all person's name is comma separated : we get an array
current_persons.push( ID ); // we add a new one in the ARRAY
$("#" + val2).val(  current_persons.join(";")  );
alert("current_persons after update : " + $("#" + val2).val());

Open in new window


and now please?
0
 
leakim971PluritechnicianCommented:
var current_persons = $("#" + val2).val(); 
current_persons = current_persons.split(";");  // all person's name is comma separated : we get an array
current_persons.push( ID ); // we add a new one in the ARRAY
$("#" + val2).val(  current_persons.join(";")  );

Open in new window

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
it works fine but my initial value have no members , so it is empty how would I remove a leading semi column
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
leakim971PluritechnicianCommented:
Correct me if I'm wrong but the code provided don't add any leading semicolumn if the array is empty

Line 4 :
["only one member"].join(";"); //return "only one member"

[].join(";"); //return ""

the following code should remove the lead sm for you :

replace line 4 with :
$("#" + val2).val(  current_persons.join(";") .replace(/^;/,"") )

Open in new window

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
line 2 add a divider. For example let say val2 have nothing in it and when i add a ID I get ; in front and then ; for each added element
0
 
leakim971PluritechnicianCommented:
>line 2 add a divider

that's wrong, it remove it and create an array of persons
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
OK. But I still divider in front of the first element
0
 
leakim971PluritechnicianCommented:
replace line 4 with :
$("#" + val2).val(  current_persons.join(";") .replace(/^;/,"") )

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
That sounds like too much work.
How did the ; get in there in the first place?
Don't push empty values
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
well I do not know That is how it shows on my debugger
0
 
leakim971PluritechnicianCommented:
var current_persons = $("#" + val2).val(); 
alert("current_persons : " + current_persons);
current_persons = current_persons.split(";");  // all person's name is comma separated : we get an array
current_persons.push( ID ); // we add a new one in the ARRAY
$("#" + val2).val(  current_persons.join(";")  );

Open in new window


what do you see in the alert box?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I just see first time  current_persons:
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
first alert box do see leading comma but in the second alert I do
0
 
leakim971PluritechnicianCommented:
Sorry, I did not understood your answer
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
so my code is . The first alert do not any leading commas , but second one does

var current_persons = $("#" + val2).val();
                                    alert("current_persons : " + current_persons);
                                    current_persons = current_persons.split(",");  // all person's name is comma separated : we get an array
                                    
                                    current_persons.push(temp2); // we add a new one in the ARRAY
                                    $("#" + val2).val(current_persons.join(","));
                                    alert("current_persons after update : " + $("#" + val2).val());
0
 
leakim971PluritechnicianCommented:
please provide a link to see the page...
no blank char in the field?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.