Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return string from javascript function and display in text area.

 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,

I want to reverse the string "somestring" and display it in the same text area when I click the link "Reverse" in my jsp page. Here is the code that I am working on, but i see no change in my jsp page. I am not sure if the javascript is returning the string. How do I go about doing this, when there is an error in the javascript or in the jsp page where do I go see?How can I display the reversed string in the same text area?


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, your JavaScript call will not work--it still needs to use JSP EL notation, and should be in quotes, since it's a JavaScript string.

Second, either in the reverse method itself (boo hiss), or in the method that takes the string as a parameter and calls reverse on it, you need to get the DOM element where you want to put it and set the text of that element.
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Second, either in the reverse method itself (boo hiss), or in the method that takes the string as a parameter and calls reverse on it, you need to get the DOM element where you want to put it and set the text of that element.




I got the first one I have changed the code for the second one you mentioned, is it correct? But then how do I get the returned string from the javascript function and display it in the text area?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you get the string from the reverse function?How familiar are you with the DOM? Do you know how to get an element from a page? Are you using a JavaScript library, and if so, which one?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm baffled: the DOM part is all here, in one of your previous threads:

http://www.coderanch.com/t/503557/HTML-JavaScript/Change-text

What, specifically, are you having a problem with?
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My specific problem, how to return the reversed string from the javascript to the jsp.



I dont use any javascript library. it is a jsp page, where my javascript function takes care of the reversing. The problem is I am able to display the reversed string using . But that is not what I want I want to return the string



to the onclick event that calls the javascript function


and display the string in the same text area


that originally had them. Hope I am clear with my problem.

Thanks for your reply.

 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I got information on how to store the returned string in a variable.



But the above code would give me an alert of the reversed string when I click the link Reverse. But what I need is display the reversed string in the same text area, that once had the original string.. Any hint please.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already *gave* you hints, and directed you to one of your own threads where you ask how to set the text of a DOM element. Set the text of the DOM element with the reversed string.
 
K West
Ranch Hand
Posts: 58
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this,

There are few changes in your function.



As David suggested read more about DOM to understand how document.getElementById('mytextarea').innerHTML works. Here is one place W3Schools

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't provide complete answers, rather guide people towards being able to figure it out for themselves. People learn much, much better that way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic