Capturing value entered in textfield of form and using in javascript code

Light Poster
2Jun2009,12:58   #1
saturn's Avatar
Hello!

I am just a beginnerin javascript,so pls need your help!

I am making a website using html.I need to capture a value from a textfield of one form in a page and use the value in an if-else loop in javascript in another page.
How to capture the value that would be given by any user and use the value to run the loop to display certain information to the user according to the value? And where should I put the script in the head section or the body section?

I have made the form as-

<form action="tariff2.html">
<center><b>Connected load:
<input type="text" name="load" size="30"></b></center>
<center><br><br><br>
<input type="submit" name="Submit" value=" Submit "
onKeyPress="return submitenter(this,event)"> &nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" name="Reset" value=" Reset "></center>

And the script as-

<script type="text/javascript" language="javascript">
var ld = load.value;
if (ld<10)
{
document.write("<b>Consumer's Category: Domestic-A</b>");
}
else if (ld>10 && ld<20)
{
document.write("<b>Consumer's Category: Domestic-B</b>");
}
else
{
document.write("<b>Consumer's Category: Commercial</b>");
}

Any help would be appreciated
Team Leader
2Jun2009,14:26   #2
pradeep's Avatar
You can post via GET and access the query string to get the value in the next page.
Go4Expert Member
2Jun2009,17:04   #3
sriram225's Avatar
Scripts in <head>
Scripts to be executed when they are called, or when an event is triggered, go in the head section.

If you place a script in the head section, you will ensure that the script is loaded before anyone uses it.

Scripts in <body>

Scripts to be executed when the page loads go in the body section.

Code:
<html>
<head>
<script type="text/javascript">
....
</script>
</head>
<body>
<script type="text/javascript">
....
</script>
</body>


I have Modified ur Code as following save this in notepad as filename with .html extension and open it works!!!!

Code:
<html>
<body>
<script type="text/javascript" language="javascript">

function cload(val)
{
 var ld =val;
if (ld<10)
 {
  document.write("<b>Consumer's Category: Domestic-A</b>");
 }
 else if (ld>10 && ld<20)
 {
  document.write("<b>Consumer's Category: Domestic-B</b>");
 }
 else
 {
  document.write("<b>Consumer's Category: Commercial</b>");
 }
}
</script>
<form action="tariff2.html">
<center><b>Connected load:
<input type="text" name="load" size="30"></b></center>
<center><br><br><br>
<input type="button" name="Submit" value="Submit" onclick=cload(load.value)> 
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" name="Reset" value=" Reset "></center>
</body>
</html>
Go4Expert Member
2Jun2009,17:08   #4
sriram225's Avatar
Code:
You can post via GET and access the query string to get the value in the next page.
Yeah ,use attribute method="get" in form and acces that value using name in nxt page
Go4Expert Member
2Jun2009,17:08   #5
sriram225's Avatar
Quote:
Originally Posted by sriram225 View Post
You can post via GET and access the query string to get the value in the next page.
Yeah ,use attribute method="get" in form and acces that value using name in nxt page
~ Б0ЯИ Τ0 С0δЭ ~
2Jun2009,17:36   #6
SaswatPadhi's Avatar
(1) Why do you repeat your posts ?!

(2) And the above quote is of pradeep, why did you change it to "Originally Posted by sriram225" ??

(3) Would you care to explain what your html code does ? I don't see anything "working" on my FF.
Go4Expert Member
2Jun2009,21:07   #7
sriram225's Avatar
Code:
 (1) Why do you repeat your posts ?!

(2) And the above quote is of pradeep, why did you change it to "Originally Posted by sriram225" ??

(3) Would you care to explain what your html code does ? I don't see anything "working" on my FF.

Lol....I thought to edit my reply, acidentally made that replies.....Cool KID
Light Poster
4Jun2009,12:28   #8
saturn's Avatar
Thank u