Solved

append selected values to the hidden form variable using jquery

Posted on 2012-03-16
16
383 Views
Last Modified: 2012-04-04
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
0
Comment
Question by:erikTsomik
  • 8
  • 7
16 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 37731550
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 37739568
it works fine but my initial value have no members , so it is empty how would I remove a leading semi column
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37739630
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 37739673
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
 
LVL 82

Expert Comment

by:leakim971
ID: 37739683
>line 2 add a divider

that's wrong, it remove it and create an array of persons
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 37739702
OK. But I still divider in front of the first element
0
 
LVL 82

Expert Comment

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

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37740749
That sounds like too much work.
How did the ; get in there in the first place?
Don't push empty values
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Author Comment

by:erikTsomik
ID: 37742720
well I do not know That is how it shows on my debugger
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37742746
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 37742760
I just see first time  current_persons:
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 405 total points
ID: 37742775
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 37742808
first alert box do see leading comma but in the second alert I do
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37742842
Sorry, I did not understood your answer
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 37742846
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
 
LVL 82

Expert Comment

by:leakim971
ID: 37742905
please provide a link to see the page...
no blank char in the field?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now