This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes How to pass integer into the javascript using struts html tags. I am able to pass String but not int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to pass integer into the javascript using struts html tags. I am able to pass String but not int" Watch "How to pass integer into the javascript using struts html tags. I am able to pass String but not int" New topic
Author

How to pass integer into the javascript using struts html tags. I am able to pass String but not int

Sachin Telang
Greenhorn

Joined: Nov 02, 2007
Posts: 19
Hello Everyone,

I am new to Struts framework. I have a simple requirement, in which I need to pass one interger value in the javascript. Which I have defined in the jsp only <%int itrcount = 0 %>, which will increment in loop. In below part of the jsp I have one <html:select> tag and I need to pass this itrcount in javascript method getValues. I am able to pass this same varibale if I used Input tag using scriptlet. but not with

Struts tag i.e. <html:select onclick="getValue(<%=itrcount%>)" >

Please help me in this regard.


Thanks<br />Sachin Telang<br />IBM Websphere Portal Application Developer Certified, Exam -399.
Sun Certified Java Programmer 1.5
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
javascript treats all variables as strings, unless specifically instructed. You need to change your code from :

<html:select onclick="getValue(<%=itrcount%>)" >

to

<html:select onclick="getValue(eval(<%=itrcount%>))" >

to get the desired result. FYI, eval() is a built-in javascript method which will convert the numeric strings to numerics.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Not remotely correct: <%= ... %> outputs the string representation of the value. The string representation of an int is an int. getValue(5) is valid JavaScript.

The first thing to check is to see if the HTML is being rendered correctly: does the onclick attribute value render correctly? If not, then there's an issue accessing the script variable (which are almost bad and unnecessary). Showing more of the surrounding code would help us provide alternative solutions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass integer into the javascript using struts html tags. I am able to pass String but not int