Solved

Correct syntax to insert Java variable into HTML?

Posted on 2010-09-02
6
507 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

760 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