Solved

Correct syntax to insert Java variable into HTML?

Posted on 2010-09-02
6
510 Views
Last Modified: 2013-11-24
if I define a Java variable "num" in a function in the header of an HTML page, how can I pass the value of that variable in a form on the same page?

example:

<form method="POST" action="mailscript.html">
    <input type="hidden" name="java_var" value="num">
    <input class="button" type="submit" value="Submit">
</form>

what is the correct syntax to insert the java variable into the HTML?
0
Comment
Question by:ob1_
6 Comments
 
LVL 3

Expert Comment

by:alanpetersen
ID: 33588892
I'm assuming that when you say you are creating an HTML page, you really mean a JSP page. In that case, you could print the value of "num" in the input element like this:

  <input type="hidden" name="java_var" value="<%= num %>" />

If you were using JSTL, you could also use Expression Language (EL) to get the variable (and make things more readable):

  <input type="hidden" name="java_var" value="${num}" />
0
 
LVL 6

Author Comment

by:ob1_
ID: 33588924
no it is an HTML page, what is JSP?
could i use this:
 
 

<input type="hidden" name="numberof" value="<script type="text/javascript">document.write(num);</script>"

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33588952
then this num is a javscript variable.
When you said 'Java Variable' it created the confusion.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Accepted Solution

by:
alanpetersen earned 250 total points
ID: 33589009
Oh, so you are using Javascript, not Java. In your question you said you were creating a Java variable. Java != Javascript

In any case, in a Javascript function, you can set the value of a field like this:
  document.getElementById('java_var').value = num

0
 
LVL 22

Assisted Solution

by:kadaba
kadaba earned 250 total points
ID: 33589197
<input type="hidden" name="java_var" value="num">
you should use an id attribute

<input type="hidden" id="java_var" name="java_var">

then the usage of
document.getElementById('java_var').value = num;
will be proper.


0
 
LVL 6

Author Comment

by:ob1_
ID: 33589292
wow i didn't even think there would be a different between java and javascript i guess that makes sense - thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 32
egit plugin on eclipse 8 40
Centering Vertically 3 28
Not seen Link button 5 17
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now