开发者

Passing a calculated variable from one function to another

开发者 https://www.devze.com 2023-02-16 04:58 出处:网络
...not sure \"calculated\" was the right word...I have written two functions, the second of which needs the output of a variable from the first. I cant seem to get it to pass...my guess is that I am c

...not sure "calculated" was the right word...I have written two functions, the second of which needs the output of a variable from the first. I cant seem to get it to pass...my guess is that I am calling it wrong, but can't seem to get it right...might have something to do with the time I've spent staring at the whole thing..

The variable I need passed is subset I am trying to use it on the last line of the second function.

If it matters, the getPos function is getting its value from an input box.

The javascript:

<script>
var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

function getPos(value)
{
    var letterPosition = alphabet.indexOf(value);
    var subset = alphabet.slice(letterPosition+1, 26);
    document.getElementById('theRest').value = subset;
}

function appendTable(id)
{
    var tbody = document.getElementById(id).getElementsByTagName("tbody")[0];
    var i = 0;
    for (var r = 0; r < 4; r++) {
        var row = tbody.insertRow(r);
        for (var c = 0; c < 4; c++) {
           开发者_如何学JAVA var cell = row.insertCell(c);
            cell.appendChild(document.createTextNode(subset[i++]));
        }
    }
}
</script>


<script>
var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var subset;

function getPos(value)
{
    var letterPosition = alphabet.indexOf(value);
    subset = alphabet.slice(letterPosition+1, 26);
    document.getElementById('theRest').value = subset;
}

function appendTable(id)
{
    var tbody = document.getElementById(id).getElementsByTagName("tbody")[0];
    var i = 0;
    for (var r = 0; r < 4; r++) {
        var row = tbody.insertRow(r);
        for (var c = 0; c < 4; c++) {
            var cell = row.insertCell(c);
            cell.appendChild(document.createTextNode(subset[i++]));
        }
    }
}
</script>

That should do the trick.
Declaring subset before the functions makes it a global var, if you define it with var subset within a function it becomes tied to that function, removing the var makes it use the global var.

0

精彩评论

暂无评论...
验证码 换一张
取 消