Solved

append selected values to the hidden form variable using jquery

Posted on 2012-03-16
16
388 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

16 Experts available now in Live!

Get 1:1 Help Now