Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

append selected values to the hidden form variable using jquery

Posted on 2012-03-16
16
Medium Priority
?
426 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

963 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