Solved

Correct syntax to insert Java variable into HTML?

Posted on 2010-09-02
6
517 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 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