Click to See Complete Forum and Search --> : How do i pass a variable from javascript to a scriptlet or to the next page


radz
10-29-2004, 03:58 AM
Hi all

On click of a button, i call a javascript function where I collect the value in a variable. i have to be able to pass this value either to a scriptlet or as a parameter to a URL . SInce i have more than two buttons i have to explicitly get the value of the button clicked.
Thanks in advance

radz

Kor
10-29-2004, 04:23 AM
You may pass a variable through the address bar similar as query.

index

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function sendV(f){
var val = f.txt.value;
location.href='nextpage.html'+'?'+val;
}
</script>
</head>
<body>
<form>
<input name="txt" type="text"><input name="" type="button" value="Send the value of text field to nextpage.html" onclick="sendV(this.form)">
</form>
</body>
</html>


and nextpage

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function getV(){
var val = location.href.split('?');
var val = val[val.length-1];
alert('The value passed is: '+val);
}
onload=getV
</script>
</head>
<body>

</body>
</html>


see also attachment

radz
10-29-2004, 04:48 AM
Thanks a million!!

It worked wonders!!

Thanks again

radz

7stud
10-29-2004, 05:12 AM
SInce i have more than two buttons i have to explicitly get the value of the button clicked.

Actually, you can get the value of the button implicitly with the 'this' keyword if you arrange things correctly:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">

<head>
<title>html and javascript</title>

<script type="text/javascript">
<!-- Hide from browsers lacking javascript

var numberOfButtons = 3;
window.onload = function()
{
for(var i=1; i <= numberOfButtons; i++)
{
document.getElementById("button" + i).onclick = sendVal;
}
}

var urls = ["page1.htm", "page2.htm", "page3.htm"];

function sendVal()
{
location.href = urls[this.value - 1] + "?code=" + this.value;
}

// End hiding -->
</script>
</head>

<body>
<form name="f" method="post" action="">
<div>Choice:<input type="button" id="button1" value="1" /></div>
<div>Choice:<input type="button" id="button2" value="2" /></div>
<div>Choice:<input type="button" id="button3" value="3" /></div>
</form>

</body>
</html>