Larry Poulin
Larry Poulin

Reputation: 29

how do I pass a variable from one script to another

Here is a script I can't get to work correctly. It's purpose is to check whether a check box is checked and then to call a second routine that responds, dependent on what the selectedindex (sindex) was shown to be when I originally called the script

<script type="text/javascript">
function checkB(ctrl,sindex) {  //get the state of the check box 
var sindex = {
    0: 0,
    1: 1,
    2: 2,
    3: 3
 };


if (ctrl.checked == true) { 
return function( which ) {
replaceContentmainobjectOn(sindex [which]);
} else {    
if (ctrl.checked == false) { 
replaceContentmainobjectOff();
}   
} 
}
</script>

here is the second script that is called

var replaceContentmainobjectOn =(function() {
var info = {
    0: 2,
    1: 1,
    2: 2,
    3: 3
 };

    return function( which ) {
    document.getElementById('ecwid-productoption-8840317-mainobject').selectedIndex = ( info[ which ] ) ;
};

}())

This is what I'm calling the first routine with

onclick="checkB(this,sindex);

Upvotes: 2

Views: 14404

Answers (1)

Petr Vostrel
Petr Vostrel

Reputation: 2332

Two individual <script> blocks share the same execution scope, the global scope. All variables you create in the global scope inside one <script> are accessible in the other.

<script>
    var a = 5;
</script>

<script>
    alert( a );
</script>

Same applies to functions.

<script>
    var b = function( c ){ return c; }
</script>

<script>
    alert( b(12) );
</script>

That you can rule-out, your problem seems to lay in the first script, which is not syntactically valid.

Upvotes: 5

Related Questions