I have the function below. It gets the values from checked boxes and transfer it to a textbox. It is working… but only if the form has 2 or more checkboxes.
JavaScript
x
25
25
1
<script type="text/javascript">
2
function sendValue()
3
{
4
var all_values = '';
5
boxes = document.DataRequest.itens.length
6
7
for (i = 0; i < boxes; i++)
8
{
9
if (document.DataRequest.itens[i].checked)
10
{
11
all_values = all_values + document.DataRequest.itens[i].value + ","
12
13
}
14
}
15
16
window.opener.document.getElementById('emailto').value = all_values;
17
self.close();
18
}
19
</script>
20
21
<form name="DataRequest">
22
<input name="itens" type="checkbox" value="name1">
23
<input name="itens" type="checkbox" value="name2">
24
</form>
25
Am I missing something to make this work with only 1 checkbox?
Advertisement
Answer
When there is one item. it does not return array
JavaScript
1
28
28
1
function sendValue()
2
{
3
var all_values = '';
4
boxes = document.DataRequest.itens.length
5
if(boxes>1)
6
{
7
for (i = 0; i < boxes; i++)
8
{
9
if (document.DataRequest.itens[i].checked)
10
{
11
all_values = all_values + document.DataRequest.itens[i].value + ","
12
13
}
14
}
15
}
16
else
17
{
18
if (document.DataRequest.itens.checked)
19
{
20
all_values = document.DataRequest.itens.value
21
22
}
23
}
24
25
window.opener.document.getElementById('emailto').value = all_values;
26
self.close();
27
}
28