Solved

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

Posted on 2012-03-21
8
213 Views
Last Modified: 2012-12-08
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
Comment
Question by:Mike
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 300 total points
ID: 37747751
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
 

Author Comment

by:Mike
ID: 37747952
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37748240
You are calling seeduserid with no parameter.  That results in a null value being returned because form.userid.value is undefined.


Cd&
0
Technology Partners: 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!

 

Author Comment

by:Mike
ID: 37748268
what parameter I should call seeduserid with to make the "seedvar" as a global variable
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37748402
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
 

Author Comment

by:Mike
ID: 38672576
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
 

Author Closing Comment

by:Mike
ID: 38672581
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net, radiobuttonlist, c# 3 69
javascript duplicates 9 26
Conformation code 4 37
Phone number mask 4 14
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question