Solved

append selected values to the hidden form variable using jquery

Posted on 2012-03-16
16
395 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

809 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