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
JavaScriptAJAXScripting Languages

Avatar of undefined
Last Comment
leakim971

8/22/2022 - Mon
leakim971

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

ASKER
erikTsomik

it works fine but my initial value have no members , so it is empty how would I remove a leading semi column
leakim971

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
erikTsomik

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
leakim971

>line 2 add a divider

that's wrong, it remove it and create an array of persons
ASKER
erikTsomik

OK. But I still divider in front of the first element
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
leakim971

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

Open in new window

Michel Plungjan

That sounds like too much work.
How did the ; get in there in the first place?
Don't push empty values
ASKER
erikTsomik

well I do not know That is how it shows on my debugger
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
leakim971

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?
ASKER
erikTsomik

I just see first time  current_persons:
ASKER CERTIFIED SOLUTION
leakim971

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
erikTsomik

first alert box do see leading comma but in the second alert I do
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
leakim971

Sorry, I did not understood your answer
ASKER
erikTsomik

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());
leakim971

please provide a link to see the page...
no blank char in the field?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes