Issue with [object HTMLInputElement] appearing in output box.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AB3004
    New Member
    • May 2009
    • 14

    Issue with [object HTMLInputElement] appearing in output box.

    Hey there, Just wanted to say Hi. - I'm new on here & have drifted here as I'm having a minor problem I was hoping someone could assist ...?
    (for some reason I couldn't post this directly into the Javascript forum, don't seem to have a 'New Thread' button)

    Am working on a 'lottery drawing program' for an assignment, so far have coded the below, but for some reason, on the output page, it keeps putting

    [object HTMLInputElemen t] into the output Text box.. - any ideas?

    Many thanks in advance, - AB.


    Code:
    <HTML>
    <HEAD>
    <TITLE>Programming : Task - Drawing numbered balls</TITLE>
    
    <SCRIPT language="JavaScript">
    
    	 // My  getRandomNumber function
    		function getRandomNumber (myNum)
    		{
    			var getRandomNumber = Math.floor(Math.random() * myNum);
    
    			// this returns the number into the alert box.
    			return getRandomNumber;
    		}			
    	
    	
    	function lotteryDraw(highBall, numberToDraw)
    	{
    		// This sets up an array called 'numberPool' which will hold your numbers to draw from
    		// the arrays size is set to the same as the user's input 'highBall'
    		var numberPool = new Array(highBall);
    		
    		// This is user entered, it sets a variable called sizeOfPool to = the user inputted 'highball'
    		var sizeOfPool = highBall;
    
    		// once the user enters the amount of numbers they wish to draw it sets up an array to hold them once they have been drawn.
    		var drawnNumbers = new Array(numberToDraw);
    		
    		/*  This is a 'FOR' loop, a loop which iterates a predetermined number of times.
    			That number is the same as  the initially entered 'highBall' The loop goes through the spaces in the array  numberPool placing 
    			incremented numbers into it's spaces ( 1, 2, 3, 4, etc...) it continues this until it reaches the number that = highBall
    			(as the array numberPool is equal to highBall, this means that  every space in the array has a number assigned to it.
    		*/
    		for (var index = 0; index < highBall; index = index + 1)
    		{
    			numberPool[index] = index + 1; 
    		}
    		
    		/*  This is a FOR loop which iterates a predetremined number of times.
    			The number of iterations is governed by the number that the user enters as  the number of balls they wish to draw (numberToDraw)
    			it randomises a number then goes through our array of numbers taking the selected number and placing it into another array ready to display. 
    			It also then removes that number from our main list of numbers to make sure that the same number isn't drawn twice'
    		*/
    		for (var drawnBall = 0; drawnBall < numberToDraw; drawnBall = drawnBall + 1)
    		{
    		// This line creates our random number between 0 & our highest possible number (sizeOfPool)
    			var randomNumber = getRandomNumber(sizeOfPool);
    		// This line adds our chosden number into the initially blank array 'Drawn Numbers'
    		// as this loop runs through, the array will slowly fill up, until all the numbers have been selected
    			drawnNumbers[drawnBall] = numberPool[randomNumber];
    		
    		// This takes our very highest number in our 'numberPool' and copies it over the number that was just selected
    		// the reason for this is so that in a minute , when the number of possible selections is shrunk by 1, the only number that is 'dropped out' is
    		// The number that was just selected and added to the array 'DrawnNumbers'
    			numberPool[randomNumber] = numberPool[sizeOfPool - 1];
    		
    		// this stops the selection process from selecting the highest number each time 
    		// as the highest number each time is written over the number just selected
    		// it ensures a full list of numbers that are all uniqwue and are no duplicates of the ones already selected.
    			sizeOfPool = sizeOfPool - 1;
    		}
    		// This writes out the contents of the array  'drawnNumbers' into the space on the HTML form.
    		document.lotteryForm.drawnNumbersTextBox.value = drawnNumbers;
    	  }
    
    
    
    </SCRIPT>
    </HEAD>
    <BODY>
    
    	<STRONG>A test of the function lotteryDraw()<BR></STRONG>
    	<FORM NAME = "lotteryForm">
    	<P>
    	Total Number of Balls in Pool:
    	</P>
    	<P>
    	<INPUT TYPE = "text" NAME = "numberOfBallsTextBox"  SIZE ="2" MAXLENGTH = "2">
    	</P>
    	<P>
    	Number of Balls to Draw:
    	</P>
    	<P>
    	<!-- Note the SIZE attribute, whose value determines the size in characters of a text box and
    	the MAXLENGTH attribute, whose value constrains the length in characters of the allowed input -->
    	<INPUT TYPE = "text" NAME = "ballsToDrawTextBox" SIZE = "1" MAXLENGTH = "1" >
    	</P>
    		
    	
    	<!-- The RESET input type creates a button which, when clicked, resets a form to its original state -->
    	<INPUT TYPE = "reset" NAME = "resetButton" VALUE = "Clear Form">
    	
    	<!-- COMPLETE THE ONCLICK EVENT HANDLER TO INVOKE lotteryDraw() WITH ARGUMENTS TAKEN FROM THE FORM INPUT TEXT BOXES -->
    	<INPUT TYPE = "button" NAME = "drawBalls"  VALUE ="Draw the Balls!"
    						ONCLICK = "highBall = document.lotteryForm.numberOfBallsTextBox;
    						numberToDraw = document.lotteryForm.ballsToDrawTextBox;
    						lotteryDraw(highBall, numberToDraw);">
    	
    	<P>
    	Drawn Numbers:
    	</P>
    	<P>
    	<INPUT TYPE = "text" NAME = "drawnNumbersTextBox" SIZE = "30" MAXLENGTH = "30" >
    	</P>
    	
    	</FORM>
    
    </BODY>
    </HTML>
    Last edited by debasisdas; May 16 '09, 06:34 PM. Reason: moved to javascript forum.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    your main problems are the input values of lotteryDraw(), these should be integers/strings but they are actually the input elements. therefore the for loop is completely skipped and the output contains the element(s) present in the array.

    tip: with the Firebug addon (Firefox) you can go step by step through your code and see, what the values are.

    Comment

    • AB3004
      New Member
      • May 2009
      • 14

      #3
      Hmm, - OK, I think I get what you mean, I'll have a crack at solving it & let you know how I get on,..

      thanks for the guidance.

      Comment

      • AB3004
        New Member
        • May 2009
        • 14

        #4
        Thank you very much to Dormilich for helping me out.
        I'm still learning JS & hadn't used many object outputs etc.

        I have now got it working so am very grateful. (on to the next one <sigh>)

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          glad you got it sorted.

          PS: wait ’til you discover, that everything in Javascript is an object…

          Comment

          • AB3004
            New Member
            • May 2009
            • 14

            #6
            Originally posted by Dormilich
            glad you got it sorted.

            PS: wait ’til you discover, that everything in Javascript is an object…
            Perfect,... Just Perfect.

            Comment

            Working...