File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to use javascript variable in jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to use javascript variable in jsp page" Watch "how to use javascript variable in jsp page" New topic
Author

how to use javascript variable in jsp page

pravin rasal
Ranch Hand

Joined: Jul 27, 2011
Posts: 63
Hello,

I have problem that I want to use javascript variable in jsp page. I use one combo box and i want to use combo box indexed value in jsp
How to use that javascript variable in jsp without submitting form.
Ashwini Kashyap
Ranch Hand

Joined: Aug 30, 2012
Posts: 90
Hi,

I will suggest you to make use of jquery rather than making use of javascript.

JQuery is much more advanced than javascript.


Thanks and Regards,
---------------------------------------------------------------------------------------
Ashwini Kashyap | akashyap@infocepts.com | www.infocepts.com
---------------------------------------------------------------------------------------
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Ashwini Kashyap wrote:Hi,

I will suggest you to make use of jquery rather than making use of javascript.

JQuery is much more advanced than javascript.


Thanks and Regards,
---------------------------------------------------------------------------------------
Ashwini Kashyap | akashyap@infocepts.com | www.infocepts.com
---------------------------------------------------------------------------------------


jQuery is a library of javascript only.


Programming is about thinking, NOT coding
Ashwini Kashyap
Ranch Hand

Joined: Aug 30, 2012
Posts: 90
Hi Vishal,

Yes thats true. "JQuery" specifically I mentioned since he might not be aware about the term/power of JQuery.


Thanks and Regards,
---------------------------------------------------------------------------------------
Ashwini Kashyap | akashyap@infocepts.com | www.infocepts.com
---------------------------------------------------------------------------------------
pravin rasal
Ranch Hand

Joined: Jul 27, 2011
Posts: 63
thanks Ashwini and vishal for reply ..but my problem is that how can i use javascript/ jquery variable in jsp page..
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
pravin rasal wrote:thanks Ashwini and vishal for reply ..but my problem is that how can i use javascript/ jquery variable in jsp page..


You use javascript variable inside the <script> tag. Look at the given tutorial
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

hi,

if you thinking assigning script variable's value to jsp (server side variable) then it's not possible. you can assing server side variable to script variable. other wise as people said you can use <script> tag to use javascript.

Regards
Jatan
Ashwini Kashyap
Ranch Hand

Joined: Aug 30, 2012
Posts: 90
Suppose you have following combo box:

<select id="fruit" name="fruit">
<option value="">Select Fruit </option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</select>

You can get selected option like:
var fruit_selected = $('#fruit').val(); // Using JQuery
$("#fruitSelected").val(fruit_selected); // Assign value of selected option into text field

Also, you can do like assign this fruit to a textfield in jsp like:
<input type="hidden" id="fruitSelected" />

For more refer to site : JQuery

Regards,
Ashwini Kashyap



Abhishk Singh
Ranch Hand

Joined: Aug 19, 2010
Posts: 43
jQuery can increase the page size.
For an alternative you can save the value in some page element using javascript.
For eg. document.getElementById("elementId").innerHTML or document.getElementById("elementId").value=<Desired value>
At later point, in some other javascript method when you want to use this value, just fetch this value using javascript.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

abhishek singhania wrote:jQuery can increase the page size.

This is usually not a problem, and using a CDN can ensure that the code is already cached. This is not a reason to avoid jQuery.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

pravin rasal, please read this article ti understand how JSP works and why you cannot use a JavaScript value in the JSP that generates the page.

This is also covered in the JspFaq. Be sure to check the FAQ first, before posting.
 
Consider Paul's rocket mass heater.
 
subject: how to use javascript variable in jsp page
 
Similar Threads
How do I populate dynamic combox items
how to access combo-box value in javascript?
HTML page refresh
populate the select when page is displayed first time
populate the combo box when the jsp page is first loaded