• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use javascript variable in jsp page

 
pravin rasal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic