aspose file tools*
The moose likes JSP and the fly likes Can't figure out how to pass a variable to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Can Watch "Can New topic
Author

Can't figure out how to pass a variable to javascript

Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34



In the onClick event, how do I send the ${i.username} value? I need to pass two variables.
Rafael Prado Oliveira
Greenhorn

Joined: Apr 30, 2013
Posts: 28
not quite sure but you will need use quotes in the ${'i.value'}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

You can't directly pass it. The value exists on the server, but the script runs on the client after the page is created and sent.

What you can do is to use JSP to create valid JavaScript syntax to be evaluated later.

See this article if that is confusing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34



How about this? It doesn't work, how can i reference to that input better? Are you positive that reading that big article will help? It doesn't seem like it.
Rafael Prado Oliveira
Greenhorn

Joined: Apr 30, 2013
Posts: 28
Stamin Adrian wrote:

How about this? It doesn't work, how can i reference to that input better? Are you positive that reading that big article will help? It doesn't seem like it.



try '${i.username}'
Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34

You we're right. After all you can reference it directly. The '${i.username}' worked. Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Stamin Adrian wrote:After all you can reference it directly.

No, you can't. JavaScript cannot directly reference scoped variables on the server. All you are doing is writing text to a buffer. That text is sent as the response with the value of the expression hard-coded into the response. That's a really important distinction. There is no direct referencing going on here.
 
Don't get me started about those stupid light bulbs.
 
subject: Can't figure out how to pass a variable to javascript