Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2013, 08:03 PM   PM User | #1
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Convert contents of a drop-down box and store them in a var

Hello,

This may seem easy to the experts out there,

But, how can I go about converting the contents of a drop down (select box) and store them into a comma separated var?

[DROP DOWN]
abdcef1
ghijklm2
nopqrs3
tuvwx4
yz0007

expected result:

var x = abdcef1,ghijklm2,nopqrs3,tuvwx4,yz0007

Much thanks and appreciation for all your help and support.

Cheers,

Jay
jason_kelly is offline   Reply With Quote
Old 01-24-2013, 08:09 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
First you'd have to declare a variable for the string (like "thisList"), get the element and find its length, then loop through every option and grab either the value or the text, then append each followed by a comma. Once done, substring(thisList.length-1) to remove the last comma.

var thisList, elem = document.getElementById("selectID"), elemLen = elem.length;
for(var i = 0; i < elemLen; i++){
thisList += document.getElementById("selectID")[i].value + ",";
}
thisList = thisList.substring(thisList.length-1);
alert(thisList);

Untested.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Users who have thanked WolfShade for this post:
jason_kelly (01-24-2013)
Old 01-24-2013, 08:26 PM   PM User | #3
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Thanks Wolf,

I couldn't get it to fire.

Code:
<!DOCTYPE html>

<html>

<head>

<script type="text/javascript">

function test() {

var elem = document.getElementById('box1').length

	for(var i = 0; i < elem i++){
		var x = document.getElementById('box1')[i].value + ",";
	}
	
	x = thisList.substring(thisList.length - 1);
		alert(x);
}

</script>



</head>

<body>
<select id="box1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select> 

<input type="submit" value="Submit" onclick="test()">

</body>

</html>
jason_kelly is offline   Reply With Quote
Old 01-24-2013, 08:57 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
You're missing some code. And some makes no sense. You don't declare "thisList" anywhere, nor append anything to it, but then you try to get a substring of it.

Code:
function test() {  
  var thisList, elem = document.getElementById('box1'), elemLen = elem.length;
  for(var i = 0; i < elemLen; i++){
         thisList += document.getElementById('box1')[i].value + ",";
     }
         var x = thisList.substring(thisList.length - 1);
         alert(x); 
}
thisList is the string to contain the list.
elem is short for element, the source.
elemLen is element length.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

Last edited by WolfShade; 01-24-2013 at 08:59 PM..
WolfShade is offline   Reply With Quote
Users who have thanked WolfShade for this post:
jason_kelly (01-24-2013)
Old 01-24-2013, 09:13 PM   PM User | #5
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Thanks Wolf.

I tried your revised code, no errors, but an alert box with just a comma appears.

Any idea?

Jay
jason_kelly is offline   Reply With Quote
Old 01-24-2013, 09:21 PM   PM User | #6
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
I'm playing around with it. Hang on.

UPDATE: Okay, this will alert ONLY what options are selected, not the whole thing (unless all are selected.)

Code:
<!DOCTYPE html>

<html>

<head>

<script type="text/javascript">

function test() {  
  var thisList = "", elem = document.forms["thisTest"].box1, elemLen = elem.length;
  for(var i = 0; i < elemLen; i++){
         if(elem.options[i].selected){ thisList += elem.options[i].value + ","; }
     }
         var x = thisList.substring(0,thisList.length - 1);
         alert(x); 
}

</script>



</head>

<body>
<form name="thisTest" id="thisTest">
<select id="box1" name="box1" multiple="multiple">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select> 

<input type="button" value="Submit" onclick="test()">
</form>
</body>

</html>
Copy/paste all the code, as I made changes to the HTML, too.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

Last edited by WolfShade; 01-24-2013 at 09:27 PM..
WolfShade is offline   Reply With Quote
Old 01-25-2013, 07:47 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
My understanding is that he wishes to place all the select options in a comma delimited string variable, not just the selected ones. (Why???)


Code:
<select id = "mysel">
<option value = "abcdef1">abcdef1</option>
<option value = "ghijklm2">ghijkl2</option>
<option value = "nopqrs3">nopqrs3</option>
<option value ="tuvwx4">tuvwx4</option>
<option value ="yz0007">yz007</option>
</select>

<script type = "text/javascript">
var x = "";
for (var i =0; i<document.getElementById("mysel").length; i++) {
x += document.getElementById("mysel").options[i].value + ",";
}
x = x.replace(/,$/,"");
alert (x);

</script>
The Egyptians were all drowned in the dessert. Afterwards Moses went up to Mount Cyanide to get the ten commandments.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:16 AM.


Advertisement
Log in to turn off these ads.