473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Select all options in Select Box.

57
Hi.

I think this might be quite an easy one for all the experts out there so here goes..

I have a select box which is dynamically created, but when it's created it is the same as a regular select box, has a name and ID. But it is only populated by about 3 or 4 options (always different when the page loads).

I would like a javascript function (say called on a button press) which then concatenates all options in the box into a var (variable), I will then create a textarea with the one long string of text in the var.

So, the option box will look like this:

[HTML]<SELECT NAME="callnotes" id="callnotes" style="visibility: hidden;">
<option value="VALUE1"></option>
<option value="VALUE2"></option>
<option value="VALUE3"></option>
<option value="VALUE4"></option>
<SELECT> [/HTML]

Then the javascript will create a var which will look like this:
'VALUE1VALUE2VALUE3VALUE4"

Then javascript will polulate a regular text area with the contents or the var.

hope that makes sense, sorry most of it is hypethetical but any straight forward code to create a varialbe based on the concatenated options in a select box would be GREAT!!

Cheers in advance.
Dec 11 '07 #1
8 1634
gits
5,390 Expert Mod 4TB
hi ...

to retrieve the concatenated text you want you may use a function like in the following example:

Expand|Select|Wrap|Line Numbers
  1. function get_values() {
  2.     var sel  = document.getElementById('callnotes');
  3.     var opts = sel.getElementsByTagName('option');
  4.     var txt  = [];
  5.  
  6.     for (var i = 0; i < opts.length; i++) {
  7.         txt.push(opts[i|.value);
  8.     }
  9.  
  10.     return txt.join('');
  11. }
  12.  
kind regards
Dec 11 '07 #2
plumba
57
hi ...

to retrieve the concatenated text you want you may use a function like in the following example:

Expand|Select|Wrap|Line Numbers
  1. function get_values() {
  2.     var sel  = document.getElementById('callnotes');
  3.     var opts = sel.getElementsByTagName('option');
  4.     var txt  = [];
  5.  
  6.     for (var i = 0; i < opts.length; i++) {
  7.         txt.push(opts[i|.value);
  8.     }
  9.  
  10.     return txt.join('');
  11. }
  12.  
kind regards
Many thanks for this, I can see the logic but cant get it working....

At the moment, to prove the concept, I'm calling this function from a standard button in the page and I've added an alert(txt); line to display the concatenated values, but I'm getting an error 'Object doesn't support this property or method', that appears with or without the alert box....
Dec 12 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

there is a syntax-error:

Expand|Select|Wrap|Line Numbers
  1. txt.push(opts[i|.value);
has to be:

Expand|Select|Wrap|Line Numbers
  1. txt.push(opts[i].value);
kind regards
Dec 12 '07 #4
gits
5,390 Expert Mod 4TB
here is a working example:

[HTML]<html>
<head>
<script type="text/javascript">
function get_values() {
var sel = document.getElementById('callnotes');
var opts = sel.getElementsByTagName('option');
var txt = [];

for (var i = 0; i < opts.length; i++) {
txt.push(opts[i].value);
}

return txt.join('');
}
</script>
</head>

<body>
<select id="callnotes">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="button" value="test" onclick="alert(get_values());"/>
</body>
</html>
[/HTML]
Dec 12 '07 #5
plumba
57
here is a working example:

[HTML]<html>
<head>
<script type="text/javascript">
function get_values() {
var sel = document.getElementById('callnotes');
var opts = sel.getElementsByTagName('option');
var txt = [];

for (var i = 0; i < opts.length; i++) {
txt.push(opts[i].value);
}

return txt.join('');
}
</script>
</head>

<body>
<select id="callnotes">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="button" value="test" onclick="alert(get_values());"/>
</body>
</html>
[/HTML]

Great, thank you. From your first post I managed to modify it so it puts the values into a var so I can play around with the results:

[HTML]<script>
function callnotes(){
var notes = document.getElementById('callnotes').options;
var opts = notes.getElementsByTagName('option');
var match = "";

for (i=0; i<opts.length; i++){
match = (match + opts[i].value);
document.getElementById("CallText").value = match;


}
//alert(match);


}
</script>[/HTML]

I do have one more question on this...

When it abtains the first line value it would contain a set number of characters, for example 6, I would like it to be filled with blanks so it takes up 10 characters, then perform the concatenations and run the same theory on the next line, so regardless of how many characters are in the options it pads out the end with spaces so it fills up to 10 characters....
Dec 12 '07 #6
gits
5,390 Expert Mod 4TB
hi ...

you may use the following function for it :)

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @param val the value to fill
  3.  * @param n no of chars to be filled
  4.  * @param chr character that should be used for the fill
  5.  *
  6.  * @return the filled value
  7.  */
  8. function left_fill_value(val, n, chr) {
  9.     var l = n - val.length;
  10.     var a = [];
  11.  
  12.     if (l > 0) {
  13.         for (var i = 0; i < l; i++) {
  14.             a[i] = chr;
  15.         }
  16.     }
  17.  
  18.     return (a.join('') + val);
  19. }
  20.  
kind regards
Dec 12 '07 #7
plumba
57
hi ...

you may use the following function for it :)

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @param val the value to fill
  3.  * @param n no of chars to be filled
  4.  * @param chr character that should be used for the fill
  5.  *
  6.  * @return the filled value
  7.  */
  8. function left_fill_value(val, n, chr) {
  9.     var l = n - val.length;
  10.     var a = [];
  11.  
  12.     if (l > 0) {
  13.         for (var i = 0; i < l; i++) {
  14.             a[i] = chr;
  15.         }
  16.     }
  17.  
  18.     return (a.join('') + val);
  19. }
  20.  
kind regards

Hi, thanks for a speedy response, but I can't quite follow this one.

So, will the above work if say the first var contains 6 characters and the sedond contains 4 characters, will it pad out the first to 10 characters (using blank spaces, then the same with the second before running the concatenation??

Cheers...
Dec 12 '07 #8
gits
5,390 Expert Mod 4TB
exactly ... the call in your code have to look like this:

Expand|Select|Wrap|Line Numbers
  1. var filled_val = left_fill_value(opts[i].value, 10, '0');
  2.  
  3. match = (match + filled_val);
kind regards
Dec 12 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: point | last post by:
Hello there... I'm a PHP programmer and starting to learn JS... I have a following problem.... I have 3 select boxes! one is hotel one is destination and one is country... if someone...
7
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses...
6
by: rajesh | last post by:
Is it possible to display the select box without scrollbar in my program there is a need for that . The code attached below contains 2 select box and four buttons and the button is used to...
9
chunk1978
by: chunk1978 | last post by:
hey everyone, i've been trying to solve this problem for 2 days straight, with no end in sight. i would greatly appreciate anyone's help. EXPLANATION: There are 3 Select Menus. The 1st and...
3
by: Beholder | last post by:
I hope that someone can help me with the following: Short background explenation: I have a shrfepoint page (newform.aspx) in a item list. On this page is a lookup column that displays a lookup...
7
chunk1978
by: chunk1978 | last post by:
hello. so i have 2 select menus which add and remove options from a 3rd select menu... it seems, however, that it's not possible to use different select menus to toggle a 3rd, because when an...
1
by: madflytom | last post by:
Hello, I'm trying to move the options of one select list to another select list. The "source" select list is divided into optgroups, and the "target" select list is not. I want to somehow keep...
1
by: madflytom | last post by:
Hello again all. I posted a question last night about dealing with <optgroups> in select boxes. We've changed directions, and now I need a little more help. Here's the scenario: Two select...
3
by: Italio Novuas | last post by:
Hi all, let me begin by saying that I *ALMOST* have this complete! What I'm trying to do is make it so my text area shows the innerHTML of any select item with the same value. For example, if I...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.