Link to home
Start Free TrialLog in
Avatar of Forrest_Gump
Forrest_Gump

asked on

send checkbox vlues with no form submition

how can i send to my js function all the values of the selected checkboxes
without any form submition
Avatar of scrathcyboy
scrathcyboy
Flag of United States of America image

function get_check_value()
{
var c_value = "";
for (var i=0; i < document.form.ckbox.length; i++)
   {
   if (document.form.ckbox[i].checked)
      {
      c_value = c_value + document.form.ckbox[i].value + "\n";
      }
   }
}
or change the  last line to this -- c_value = document.form.ckbox[i].value;
if you don't want to put all the values into a list.
Avatar of Forrest_Gump
Forrest_Gump

ASKER

<input type="CHECKBOX" name="menu" value="1">1
<input type="CHECKBOX" name="menu" value="2">2
<input type="CHECKBOX" name="menu" value="3"> 3

for example the use choosed 2+3
thant the my function "getcustomer()" need to receive (2,3)
how my link need to look like?

all i need is some link that will send to my function all the choosen checkboxes values
suppose i go your way:
c_value = c_value + document.form.ckbox[i].value + ",";

in c_value i will have some thing like this:
4,6,9

i still need to send those to my function...and to call somehow to your function too...
i am not sure with the syntax i guess something like: showcustomer(c_value)?
how can i call to your function with a link and not from the "<form>"?
try this --

DoTheCheck()
if string { SendToCustomer(string) }


function DoTheCheck() {
   var string = '';
   if(document.menu[0].checked == true)
   string += document.menu[0].value + ', '
   if(document.menu[1].checked == true)
   string += document.menu[1].value + ', '
   if(document.menu[2].checked == true)
   string += document.menu[2].value + ', '
return string;
}

That function will return --   1, 2, 3 if all are checked
                                             1, 2   if only first 2 checked
                                             ' ' nothing if none are checked

then SendToCustomer() sends which boxes are checked separated by commas, or nothing if nothing is checked.

if you don't want spaces before the next number, use ',' instead of ', '  (the latter has a space)
Is that what you want?
i don't know how to call to this function:(
i am not using formtag...
and i never know how many checkboxes are there...its being generated with a loop...
imagen list of checkboxes (each for name of customer, there can be new customer name added\deleted each second)
the user need to select only part of them and go on...
that is why I listed the first function, it will LOOP through ALL checkboxes, and add their values to the variable c_value.  All you have to do at the end of that function is to -- return c_value;
thankyou for helping me...
please can you show me how i am calling to those functions?(i got no form tag)
i need it to be called from a link or a button without <form>
suppose i have
<input type="CHECKBOX" value="1">1
<input type="CHECKBOX" value="2">2
<input type="CHECKBOX" value="3"> 3

and my final function that has to their values calles getcustomer()

how i am calling to your loop function?
<A href ="javascript:return get_check_value();if return getcustomer(return)";> find values</A>
this is the exact syntax?


<script src="selectcustomer.js"></script>

<script language="JavaScript">
function get_check_value()
{
var c_value = "";
for (var i=0; i < document.form.ckbox.length; i++)
   {
   if (document.form.ckbox[i].checked)
      {
      c_value = c_value + document.form.ckbox[i].value + "\n";
      }
   }
}
</script>


<A href ="javascript:return get_check_value();if return getcustomer(return)";> find values</A>
document.form.ckbox
doesn't it means that i have to use <form>?

because my checkboxes look like this:
<input type="CHECKBOX" name="menu" value="1">1
<input type="CHECKBOX" name="menu" value="2">2
<input type="CHECKBOX" name="menu" value="3"> 3
do i need to change the checkboxes names or id's?
i run the code above and get the error:
'return' statement outside the function
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes i know i was desperate yesterday...sory about it...wont happend again
Why checkboxes. In your other questions you had radios.
i will have the same problem with the radio but it is less important as the checkboxes.
No, because with radios you just do

<input type="radio" name="rad" value="customer1" onClick="showCustomer(this.value)" />
<input type="radio" name="rad" value="customer2" onClick="showCustomer(this.value)" />
<input type="radio" name="rad" value="customer3" onClick="showCustomer(this.value)" />