Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    93
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Problem with randomiser script

    Hi, I'm trying to implement a randomise function for question numbers in a test generator i'm developing. I've picked up a script which I've modified, however, i'm fairly new to javascript and i don't think i've set it up properly. I have a dynamic array of text fields which contain each question number. I want to use onclick to call the randomiser to reorder the questions while making sure they are all unique. I've placed the code in the <head> section, but i get the error that "div has no properties" - even though i am accessing the same <div> for another piece of js validation. Any help would be greatly appreciated.

    Code:
    <script type="text/javascript">
    <!-- Begin
    /* This script and many more are available free online at
    The JavaScript Source!! http://javascript.internet.com
    Created by: Barry Pranklin :: http://www.pranklin.com */
    
    var Found=false
    var div = document.getElementById("preview");
    var fields = div.getElementsByTagName("input");
    var len = fields.length;
    var num = len -1; 
    var Current = new Array();
    var MaxValue=len; 
    var NumUnique=len ; 
    var Count=0;
    var Current = new Array(NumUnique);
    GetUnique();
    alert("The unique numbers are: " + Current[0] + ", " + Current[1] + ", and " + Current[2])
      function GetUnique()
      {
        for (i=0;Count<NumUnique;Count++)
        {
          Found=false
          var rndValue = get_random()
          var j=0
          for (j=0;j<Current.length;j++)
          {
            if (Current[j] == rndValue)
            {
              Found=true
              break
            }
          }
          if (Found)
          {
            Count--
          } else {
            Current[Count]=rndValue
          }
        }
      }
      function get_random()
      {
        var ranNum= Math.round(Math.random()*MaxValue);
        return ranNum;
      }
    // End -->
    </script>
    calling it here....this may be problem but not sure how to resolve it
    Code:
    <input name="Randomise" value="Randomise question order" onclick="GetUnique(this)" type="submit" title="Submit" />

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Put the script below the form in the HTML, maybe just before the closing body tag.

    It will fail in the head, or before the form, because the script is looking for the id 'preview'
    ( most likely your form id ) to get the input elements length:
    Code:
    var div = document.getElementById("preview");
    var fields = div.getElementsByTagName("input");
    If the form is not loaded before the script, the attempt to set the variable 'div' will fail, because it does not exist yet.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    You are trying to access the div before it has been created. You need to have a function that initialises the variables after the page has loaded.
    Try this:
    Code:
    <html>
     <head>
      <title> New Document </title>
      <script type="text/javascript">
    <!-- Begin
    /* This script and many more are available free online at
    The JavaScript Source!! http://javascript.internet.com
    Created by: Barry Pranklin :: http://www.pranklin.com */
    var Found=false
    var div;
    var fields;
    var len;
    var num; 
    var Current = new Array();
    var MaxValue; 
    var NumUnique; 
    var Count=0;
    var Current = new Array();
    function generateRands(){
     div = document.getElementById("preview");
     fields = div.getElementsByTagName("input");
     len = fields.length;
     num = len -1; 
     MaxValue=len; 
     NumUnique=len ; 
     Current = new Array(NumUnique);
     GetUnique();
     alert("The unique numbers are: " + Current[0] + ", " + Current[1] + ", and " + Current[2])
    }
      function GetUnique()
      {
        for (Count=0;Count<NumUnique;Count++)
        {
          Found=false;
          var rndValue = get_random();
          var j;
          for (j=0;j<Current.length;j++)
          {
            if (Current[j] == rndValue)
            {
              Found=true
              break
            }
          }
          if (Found)
          {
            Count--
          } else {
            Current[Count]=rndValue
          }
        }
      }
      function get_random()
      {
        return ranNum= Math.round(Math.random()*MaxValue);
      }
    // End -->
    </script>
     </head>
    <body onload="generateRands()"> 
    <input name="Randomise" value="Randomise question order" onclick="generateRands()" type="submit" title="Submit" />
    
    <div id="preview">
    <input type="text" name="test1"><br>
    <input type="text" name="test2"><br>
    <input type="text" name="test3">
    </div>
    </body>
    </html>
    Also your call to GetUnique() in the submit button doesn't accept any variables so loose the "this". Calling GetUnique will generate new numbers each time but the function just assigns the numbers to a variable so you won't see any changes.

  • #4
    New Coder
    Join Date
    Aug 2007
    Posts
    93
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Many thanks for your replies - I've taken your advice and implemented the following script which works perfectly.

    Code:
    <script type="text/javascript">
    
    var Found=false
    var div;
    var fields;
    var len;
    var num; 
    var Current = new Array();
    var MaxValue; 
    var NumUnique; 
    var Count=0;
    var Current = new Array();
    function generateRands(){
     div = document.getElementById("preview");
     fields = div.getElementsByTagName("input");
     len = fields.length;
     num = len -1; 
     MaxValue=len-1; 
     NumUnique=len ; 
     Current = new Array(NumUnique);
     GetUnique();
     //loop through fields, updating with randomly generated number
     for(var i=0;i<len;i++)
     {
     fields[i].value=Current[i];
     }
    }
      function GetUnique()
      {
        for (Count=0;Count<NumUnique;Count++)
        {
          Found=false;
          var rndValue = get_random();
          var j;
          for (j=0;j<Current.length;j++)
          {
            if (Current[j] == rndValue)
            {
              Found=true
              break
            }
          }
          if (Found)
          {
            Count--
          } else {
            Current[Count]=rndValue
          }
        }
      }
      function get_random()
      {
        return ranNum= Math.round(Math.random()*MaxValue+1);
      }
    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •