PDA

View Full Version : [SOLVED] how to pass values in javascript?



abhilashm86
September 25th, 2009, 02:07 PM
i can call a function from <body> in javascript, can i pass values to a function? check this.....
<html>
<head>
<title>
mul table
</title>
</head>

<script language="javascript">

function abhimul() {
var abhi=prompt("enter a number");
document.write("you entered " +abhi+ "<br>");

for(var i=1; i<=10; i++)
{
document.write(abhi +" * " + i + "=" +(i*abhi)+ "<br>");
}

}
</script>

<body>
<a href="javascript:abhimul()">know tables</a>
</body>

</html>


if i want to send values, i tried but din't work....


<body>
var abhi=prompt("enter a number");
document.write("you entered " +abhi+ "<br>");
abhimul(abhi);
</body>

in called function, can i recieve it as abhimul(var abhi) ?
how to pass values?

CyberJack
September 25th, 2009, 02:46 PM
You try to use javascript without script tags in the body of your html page. That is not going to work.
From the example I guess you want the user to enter a number and let javascript use the given number.

Javascript functions can have parameters like so:


<script type="text/javascript">
function test(param1, param2)
{
alert( param1 +' '+ param2 );
}
</script>


Now you can call the javascript function the way you did in your first example, only now with 2 parameters.
This will result in a alert with the message "test param1 test param2'


<a href="javascript:test('test param1', 'test param2')">link</a>


So I guess this is what you want:


<script type="text/javascript">
function askNumber()
{
var number = prompt("enter a number");
alertNumber( number );
}

function alertNumber( number )
{
alert( number );
}
</script>

Then from you html body you create a link calling the "askNumber" function the same way you did in your first example.

More on javascript function: http://www.w3schools.com/jS/js_functions.asp

abhilashm86
September 25th, 2009, 04:21 PM
You try to use javascript without script tags in the body of your html page. That is not going to work.
From the example I guess you want the user to enter a number and let javascript use the given number.



thanks a lot, i made a very bad mistake, din't know javascript is this much syntax oriented!!
see this,

<script type="javascript/text">
function test(p1,p2)
{
alert(p1+ ' '+ p2);
}

</script>
i had put "javascript" before text, i was scratching my head!! you gave a link w3schools, it saved!!
also i tried doing same to above program, but its not taking parameters,

<html>
<title>
mul table
</title>

<body>
<script type="text/javascript">

function abhimul(abhi)
{
for(var i=1; i<=10; i++)
{

document.write(abhi +" * " + i + "=" +(i*abhi)+ "<br>");
}
}
</body>
</script>


<body>
<script type="text.javascript>
<a href="javascript:abhimul(10);">get-tables</a>
</script>
</body>
</html>

what is wrong now, if i add <script type> to end of <body>, there is error.....

abhilashm86
September 25th, 2009, 04:29 PM
i figured out error!! sorry, script needs to closed before body...........

<html>
<title>
mul table
</title>

<body>
<script type="text/javascript">

function abhimul(abhi)
{
for(var i=1; i<=10; i++)
{

document.write(abhi +" * " + i + "=" +(i*abhi)+ "<br>");
}
}
</script>
</body>

<body>
<script type="text/javascript">
function abhitake()
{
var abhi=prompt("enter a number");
abhimul(abhi);
}

</script>
</body>


<body>
<a href="javascript:abhitake();">get-tables</a>
</body>
</html>



now i'l try using a good editor than plain text........