Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sachin Telang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic