*
The moose likes HTML, CSS and JavaScript and the fly likes How Can I Maniuplate This Field via JavaScript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How Can I Maniuplate This Field via JavaScript?" Watch "How Can I Maniuplate This Field via JavaScript?" New topic
Author

How Can I Maniuplate This Field via JavaScript?

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I have a field on my form named "labExceptionBean[0].markedForDeletion". Is there any way I can manipulate that field?

The reason for the odd naming is due to the fact that I'm using indexed fields in Struts and that's the naming convention used. I don't get to pick the name - it's picked for me.

My problem is that, if I try to do this:



It tries to index the array labExceptionBean, which isn't an array at all. Of course, this subsequently fails.

What I'm trying to accomplish is to make a simple "select all" button for a series of checkboxes on my form. I'll have some variable number of checkboxes on the form, but they'll be named as such:

labExceptionBean[0].markedForDeletion
labExceptionBean[1].markedForDeletion
labExceptionBean[2].markedForDeletion
labExceptionBean[3].markedForDeletion
...

Any ideas? I seem to be fresh out.


SCJP Tipline, etc.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The reason why it does not work is because it sees it was more items down the document tree. What you need to do is use the form/element array with the name.

Basic idea:



Here is the basic idea I tested it with:


Hope that helps
Eric
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Awesome. Muchas gracias, senor. (That's Spanish for Many gracias. )

Here's my final function:



Works like a charm. Thanks, Eric.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Note - I put the goofy periods in the "EVAL" statements above because the Ranch doesn't like the eval term in posts.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
This is a way that I would approach it:



Reasons why I would do it this way, eval tends to lag down large loops. Down fall to this method above is if you have tons of form elements, it has to loop through them all, which can also be slow.

Eric
Manuel Sanchez Iba�ez
Greenhorn

Joined: Jul 12, 2005
Posts: 20
Hello, i just have the same problem with Struts. Thanks of this post, i have found one solution but now i have a new problem.

I have a Form with one <html:select> and severals fields <html:radio> associated. When i click in one radio field, the list of values change in a select field (others values).

Contenido : <html:select property="contenido" size="1"></html:select>

<html:radio property="tipocontenido" value="conindata" onclic="changeContenidosEjercicio(this.value);"/>Conducta

<html:radio property="tipocontenido" value="gesindata" onclic="changeContenidosEjercicio(this.value);"/>Gesto

I can realize this operation fine with this javascript function (Previosly i have loaded in various arrays (Arrayconindata,Arraygesindata) the diferent values:
------------------------------------------------------
function changeContenidosEjercicio(control){
contenidosArray = new Array();
contenidosEjercicio = document.forms['ejercicioForm'].contenido;
contenidosEjercicio.options.length=0;

if (control == 'conindata') {contenidosArray = Arrayconindata;}
if (control == 'gesindata') {contenidosArray = Arraygesindata;}

for (i=0; i < contenidosArray.length;i++)
contenidosEjercicio.options[i] = contenidosArray[i];
}
-------------------------------------------------------------


Now, i have to recover several beans and show it in a jsp with their values stored. For realize this, i have used indexed="true" in a fields and the javascript function have been modified a litle (adding a index):

<logic:iterate id="controles" name="listaejercicios" indexId="contador">

Contenido : <html:select name="controles" property="contenido" indexed="true" size="1"></html:select>

<html:radio name="controles" property="tipocontenido" value="conindata" indexed="true" onclic="changeContenidosControl(this.value,this.name);"/>Conducta

<html:radio name="controles" property="tipocontenido" value="gesindata" indexed="true" onclic="changeContenidosControl(this.value,this.name);"/>Gesto I
.....


----------------------------------
function changeContenidosControl(control,name){
var contenidosArray = new Array();
var ind = name.substring( name.indexOf('[') +1 ,name.lastIndexOf(']') );
var id = parseInt(ind,10);

var contenidosControl = document.forms['controldinamicoForm'].elements["controles["+ id +"].contenido"];
contenidosControl.options.length = 0;

if (control == 'conindata') {contenidosArray = Arrayconindata;}
if (control == 'gesindata') {contenidosArray = Arraygesindata;}

for (i=0; i < contenidosArray.length;i++)
contenidosControl.options[i] = contenidosArray[i];

}


If i show various records (beans) in a jsp, if i only ckecked one radio button works fine, but if i ckecked the same radio button of other record (bean) throws an error: "the object don�t accept this property or method"

Sorry for my english. Any idea?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Can I Maniuplate This Field via JavaScript?
 
Similar Threads
Retaining value of checkbox on the form
commons fileupload mutlipart/data-form, extracting values from the checkboxes
Struts Form Validation - Property Array
input type="checkbox" boolean value
Checkbox List