Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascrpt function for check boxes 2

Status
Not open for further replies.

hem3698

Programmer
Jun 17, 2005
9
GB
hi friends,
i am replacing radio buttons with check boxes in my jsp page.
i need java script function to differentiate between the boxes that are checked and the boxes that are not checked.
i mean if the box checked it should be true and the form should be submitted to the action class from the jsp(i am using Struts).

please any one help me with the sample code as i am struggling with this.

your replies are much awaited and appreciated


regards and thanks in advance
 
>>i mean if the box checked it should be true and the form should be submitted to the action class from the jsp(i am using Struts).


confusing...

Known is handfull, Unknown is worldfull
 
actually i check boxes should be validated using java script function.
i need some sample code in java script that verifies whether the check boxes are checked or not?
can you please help me with some sample code?

regards and thanks in advance
 
The following code checks to see if the checkbox named "checkboxName" in the form named "formName" is checked:
Code:
if (document.forms['formName'].elements['checkboxName'].checked) // do something
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
if (document.forms['formName'].elements['checkboxName'][INDEX].checked) // do something, INDEX is meant if there is more than once checkbox


Known is handfull, Unknown is worldfull
 
I do not understand why you added another [], vbkris. We're talking checkboxes here (not radio buttons). Unless I'm totally missing what you are suggesting?

Cheers,
Jeff


[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
You're not wrong, Jeff. The second index is needed ONLY if multiple checkboxes share the same name.

As we do not know for sure what the poster's code is like, we can't assume one way or the other... however, it is more common for checkboxes to have unique names than not.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
>>The second index is needed ONLY if multiple checkboxes share the same name

yup, i alwasy use the same name for checkboxes (as it becomes easy for reading server side)...

Known is handfull, Unknown is worldfull
 
Cheers for pointing that out (about checkbox names)... I guess I use them so infrequently that I never thought to use the same name! Very interesting idea -- calling them all the same for easy server-side parsing.

I don't quite know if I'll use that technique immediately... but it certainly bears consideration.

Cheers - and you can both have a star on me [smile]
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
thanks jeff...

Known is handfull, Unknown is worldfull
 
hi vbkris,
is INDEX an integer?
whre should i define Index in the jsp?
regards and thanks in advance
 
>>is INDEX an integer

yes it is.

e.g.:

<Input type="checkbox" name="Chk1" value="1">
<Input type="checkbox" name="Chk1" value="2">

now u call it in js:

alert(document.forms[0].Chk1[0].value)


in ur JSP read it as u would read any normal field (i forgot the function, be glad if u could fill it up)...

Known is handfull, Unknown is worldfull
 
hi vbkris,
my problem is i am using checkboxes with same name.
so i need the java array of all the check boxes that are checked and should be send to the action class from jsp.
can you please tell me where should i add the elements checked to the array?
can you please help me with some sample code?

regards and thanks in advance
 
>>so i need the java array of all the check boxes that are checked and should be send to the action class from jsp.


can u give me the command for retrieveing values in POST method of JSP?

all u have to do is REQUEST("Chk1")

this will give u a comma seperated value of the checkboxes selected like:
1,3,5

now all u have to do is split them at ",".

you now have an Array!!!

>>should be send to the action class from from JSP

what does that mean? can i have the process flow with examples???

Known is handfull, Unknown is worldfull
 
hi vbkris,
this is what i am getting to my action class

int movementIndex = Integer.parseInt(getParameter(req,"movementIndex"));

where "movementIndex" is the name i used in the jsp like this


<td width="5%" valign="middle" align="center" height="1%">
<font face="Arial" size="2">
<input type="checkbox" name="movementIndex" value="<%=""+(checkboxIndex++)%>">
</font>
</td>

where int checkboxIndex = 0;

now my question is in the following code

int movementIndex = Integer.parseInt(getParameter(req,"movementIndex"));

i will get an integer. it will be 3 or 6 or 2 etc,

but if i select multiple checkboxes how does it work

i will be waiting for your reply mate

regards and thanks in advance

 
for multiple:

String AllValues=getParameter(req,"movementIndex");
AllValues=AllValues.split(",");
int i=0;
for(i=0;i<AllValues.length;i++)
{
if(AllValues!="")
{
int movementIndex = Integer.parseInt(AllValues);
out.println("This is the value - "+movementIndex);
}
}



Known is handfull, Unknown is worldfull
 
hi vbkris

where should i write this?

is in my action class?
 
hi vbkris
i am using java 1.2 there is no method called split for String class.

is there any other way to achive this?

regards and thanks in advance
 
why are u using a lower version???

in javascript i would do it like this:
<script>
AllValues="1,2,3";

i=0;
while(AllValues.indexOf(",")!=-1)
{
alert(AllValues.substring(0,AllValues.indexOf(",")))
AllValues=AllValues.substring(AllValues.indexOf(",")+1,AllValues.length)
}
if(AllValues!="")
alert(AllValues)
</script>

for a better approach u can try the Java forum...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top