Link to home
Start Free TrialLog in
Avatar of erikTsomik
erikTsomikFlag for United States of America

asked on

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
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Avatar of erikTsomik

ASKER

it works fine but my initial value have no members , so it is empty how would I remove a leading semi column
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

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
>line 2 add a divider

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

Open in new window

That sounds like too much work.
How did the ; get in there in the first place?
Don't push empty values
well I do not know That is how it shows on my debugger
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?
I just see first time  current_persons:
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
first alert box do see leading comma but in the second alert I do
Sorry, I did not understood your answer
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());
please provide a link to see the page...
no blank char in the field?