3

i want to use multiple function in a script.i am not getting calculated value in second textbox. i dont know what is wrong in my program. returning no value.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function fun1()
{
    var z=5;
    function fun3(x)
    {
        alert("i am fun3");
        var y=x+z;  
    }
    return y;
}
function fun2()
{   
    var a = document.getElementById("txt1").value;
    var result = fun3(a);
    document.getElementById("txt2").innerHTML=result;
}
</script>
</head>
<body>
Enter no: <input type="text" value="" id="txt1" onkeydown="fun2();">
Result: <input type="text" value="" id="txt2" /> 
</body>
</html>
2
  • 2
    fun3 can't be called from fun2 because it is defined in fun1 and therefore only available in that scope. Move it beside fun1/fun2 Commented Jul 3, 2015 at 9:52
  • u r right but actually requirement is like .. i have some value in fun2(). and some value in fun3(). task is to calculate both values and to get result in textbox2. Commented Jul 3, 2015 at 9:58

2 Answers 2

2

Are you looking for something like this ?

  function fun1(a)
    {
        var z=5, y;
        function fun3(x)
        {
            alert("i am fun3");
            y=x+z;  
        }
        fun3(a)
        return y;
    }
    function fun2()
    {   
        var a = document.getElementById("txt1").value;
        var result = fun1(a);
        document.getElementById("txt2").value=result;
    }

EDIT :

I changed : document.getElementById("txt2").innerHtml=result; with document.getElementById("txt2").value=result; as txt2 is an input If not, please precise your question, I'll edit it as soon as I've more details.

EDIT 2

@Nitish finished by found by himself : jsfiddle.net/nitishkaushik/4sxb9d55/4

Sign up to request clarification or add additional context in comments.

5 Comments

sir i want (a+z) in 2nd textbox.and condition is, value of a is in fun2(). and value of z is in fun1().
but result is not calculated as we press key...can u do it in jsfeedle
when press 2 in firsttextbox,output should be 7, again when press 4 in first textbox output should be 9
got my answer....i modify your code a little bit more... your code was very helpfull tanx :)
Could you give a JSFiddle link then I edit my post quoting you to have a complete answer for people coming next time ?
0

this is what i want. and i got it. if anybody want then they can use :)

Enter no: Result:

<script>
function fun1(a)
{
    alert("Debugging 1st level="+a);
    var z=5, y;
    function fun3(x)
    {
    alert("Debugging 2nd level="+x);
         y= (parseInt(x) + parseInt(z));  
    alert("Debugging 3rd level="+y);
    }
    fun3(a)
    return y;
}

function fun2(val)
{   var result=0;

    alert("value is"+val);
    var result = fun1(val);
    alert("Debugging 4th level="+result);
    document.getElementById("txt2").value=result;
}

</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.