File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Pass selected bean property to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Pass selected bean property to javascript" Watch "Pass selected bean property to javascript" New topic

Pass selected bean property to javascript

Brian R. Wainwright
Ranch Hand

Joined: Aug 12, 2003
Posts: 92
Hello Everyone,

I have the following on a JSP:

What I'd like to do is pass the filePath property of the currently selected bean (critTone) in the select list to the playTone() javascript. I'm having a hard time trying to get my head around what the best approach would be since I'm not refrencing the option value. The way it is written above, the value being passed to the playTone() function is the filePath of the last bean in the iteration.

This has to be a fairly common thing to do... does anyone have any suggestions?

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd suggest using server-side code that loops through your list and creates a JavaScript associative array containing tone Ids and their corresponding paths. In case you're not familiar with it, an associative array in JavaScript works very much like a HashMap in Java. Below, I've hard-coded an example of what the array would look like when it's rendered:

Then you can get the path with this line:

var path = tonesAndPaths[document.myForm.criticalAlertId.value];
[ August 03, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
Brian R. Wainwright
Ranch Hand

Joined: Aug 12, 2003
Posts: 92
Thanks Merrill,

I came up with an idea that was similar to your implementation using a javascript array, but it runs on the client...

And then elsewhere on my jsp...

So when the page laods, I iterate through my list and populate a Javascript array with the values I need to retrieve. In my hyperlink, I call playTone() whcih takes the name of the particular element. I then get the selected index of that element and based on that look up the value by index in my prepopyulated array. Works great.
I agree. Here's the link:
subject: Pass selected bean property to javascript
It's not a secret anymore!