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