Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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>
0
Mike
Asked:
Mike
  • 4
  • 3
1 Solution
 
COBOLdinosaurCommented:
To be global he var must be declared outside of a function.

do it this way:
<script type="text/javascript">
var seedvar;
                 
function seeduserid (form)
      {
            seedvar = form.userid.value;
            //var mrnot = new MersenneTwister(seedvar);
            //alert (mrnot.random());
            //alert (seedvar);
            return(seedvar);
      }



Cd&
0
 
MikeAnalystAuthor Commented:
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"
0
 
COBOLdinosaurCommented:
You are calling seeduserid with no parameter.  That results in a null value being returned because form.userid.value is undefined.


Cd&
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
MikeAnalystAuthor Commented:
what parameter I should call seeduserid with to make the "seedvar" as a global variable
0
 
COBOLdinosaurCommented:
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&
0
 
MikeAnalystAuthor Commented:
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.
0
 
MikeAnalystAuthor Commented:
Didn't get the exact answer that I needed but "To be global he var must be declared outside of a function." did help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now