Solved

append selected values to the hidden form variable using jquery

Posted on 2012-03-16
16
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell PGP and SFTP 1 48
How does GeoJson relates to Leaflet and Mapbox? 4 50
Woo-commerce gallery not working properly 12 42
Add Log to powershell Script 14 30
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

752 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