0

I'm using struts. My ActionForm has an ArrayList set up within it, how can I access the array from the JSP that the ActionForm is sent to by the controller in jQuery on a button click. This is so I can loop through the elements of that array. I guess its something like this, but that is a stab in the dark (which isn't working).

$('myButton').click(function(){
    var myArrayToLoopThrough = $('myForm.myArray');
    for(){
        //looping stuff
    }
}
edwardmlyte
  • 15,937
  • 23
  • 58
  • 83

4 Answers4

0

jQuery operates on the HTML generated by your JSP.

So have a look to the generated HTML in the browser using a tool like Firebug for Firefox.

Then you can use jQuery to select and iterate over HTML elements. Here are the basic syntax of the most useful stuff:

Select an ID: $("#id")

Select by class: $(".class")

Select by HTML tag: $("p") or $("span")

Iterate over a selection

$(...something...).each(function(){
   // this is the DOM element
   // $(this) is a jQuery object containing the DOM element
});

Official jQuery documentation on selectors


EDIT

Based on your comments, you seem to be looking for a way to communicate with server objects instead of generated HTML.

Javascript (jQuery is written in Javascript) is a web browser language that can only interact with generated HTML. Your Java objects are not sent to the browser.

If you do need to retrieve data from the server, then you need a new HTTP request in order to retrieve those data. This can be done in jQuery using the AJAX methods.

Vincent Robert
  • 35,564
  • 14
  • 82
  • 119
  • Sorry. My array doesn't have an ID to access it with. Just a getter from the object that holds it. How can I access the getter? Would that be something like: var array = $('myForm.getArray()'); – edwardmlyte Nov 18 '10 at 15:50
  • It sounds like you want var array = $(this).closest('form').serializeArray() – Bill Criswell Nov 18 '10 at 15:59
  • No its not the HTML form object I want to access. Its the Struts form I want to access. I think my original query was misleading sorry. – edwardmlyte Nov 19 '10 at 07:30
  • Can you elaborate on your need in the question. Maybe an example of data you want to retrieve. I edited my answer to expand possible solutions. – Vincent Robert Nov 19 '10 at 07:39
  • Thanks Vincent. Although I didn't use your method in the end it does look a lot better. I have temporarily concatenated the IDs of the data I want to check and passed them as a hidden field into the HTML form and I'll split them in JavaScript to do my work. All I needed to do was look at the ID's anyway. – edwardmlyte Nov 19 '10 at 09:53
0

You may want to check out .serializeArray(). You can get all the data from the form into a nice object so you can do what you want with the data.

jQuery .serializeArray() Documentation

var data = $('#form-id').serializeArray();

Now you can loop through data. The keys are name and value.

Bill Criswell
  • 32,161
  • 7
  • 75
  • 66
0

Is this what you're looking for?

$('.myButton').click(function(e) {
    var data = $(this).closest('form').serializeArray();
    for( var i = 0; i < data.length; i++ ) {
        var field = data[i];
        console.log( field.name + '=>' + field.value );
    }
});
Bill Criswell
  • 32,161
  • 7
  • 75
  • 66
0

Obtain form input fields using jQuery?

Community
  • 1
  • 1
Eduardo
  • 7,631
  • 2
  • 30
  • 31
  • No. I think my question was poorly worded. I don't need to access the HTML form. With struts you have an Action and Form. The form is just a bean with getters and setters. I want to access the getters for that form, not the HTML from on the jsp. – edwardmlyte Nov 19 '10 at 07:29