Link to home
Start Free TrialLog in
Avatar of Mike
Mike

asked on

Passing Form textbox value as a global varibale to be used by multiple javascript function

I couldn't get it going.. I have input textbox, want to pass the value (userid) as a global varibale, so I can use them as a seed for my random function generator then I can use them in multiple javascripts function.

I am able to pass it as a local varible but couldn't get it out as global varible. Can you help?



<script type="text/javascript">
                  
function seeduserid (form)
      {
            var seedvar = form.userid.value;
            //var mrnot = new MersenneTwister(seedvar);
            //alert (mrnot.random());
            //alert (seedvar);
            return(seedvar);
      }
      
      //var mrnot = new MersenneTwister(100)
      var mrnot = new MersenneTwister(seedvar)
      
      
      var x = mrnot.random()*451;;
      & other varibales and fuctions....
      
<SCRIPT>

<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="transForm">
<p> User ID: <INPUT TYPE="text" NAME="userid" id="userid" VALUE="" onblur="seeduserid(this.form)">
</FORM>
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike
Mike

ASKER

I understand it has to be outside the function, but what I want is the global variable update it selelf to the value returned form the function.

function seeduserid (form)
      {
            seedvar = form.userid.value;
            //var mrnot = new MersenneTwister(seedvar);
            //alert (mrnot.random());
            //alert (seedvar);
            return(seedvar);
      }

var seedvar1 = seeduserid();

var mrnot = new MersenneTwister(seedvar1)


It still not settting the global variable "seedvar1" to "userid"
You are calling seeduserid with no parameter.  That results in a null value being returned because form.userid.value is undefined.


Cd&
Avatar of Mike

ASKER

what parameter I should call seeduserid with to make the "seedvar" as a global variable
It becomes a global by being created outside of any function.  It mus exist before you reference it in a function.

the problem I was referring to was: var seedvar1 = seeduserid();  which cannot possibly return a value without a formname for a parameter.

The proper way to do it is to give the form an id and reference the id instead of this.form you could then use document.getElementById('idofform')


Cd&
Avatar of Mike

ASKER

In March I have completed the task but forgot exactly how I did it now.

However, for this particular question, what COBOLdinosaur (Posted on 2012-03-21 at 14:57:54ID: 3774775) suggested  "To be global he var must be declared outside of a function. " did work. Closing this question.
Avatar of Mike

ASKER

Didn't get the exact answer that I needed but "To be global he var must be declared outside of a function." did help.