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 6 of 6

Thread: Javascript

  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript

    Got it working
    Last edited by codingcodeddode; 11-10-2012 at 08:34 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    It doen't work because the method document.write( ) does *NOT* return any value.

    SO when you do
    Code:
    var randomNumber = document.write(Math.floor((Math.random()*5)+4));
    indeed that writes the number to the screen, but then it assigns the result of calling document.write( ) to your variable. But, as I said, that result is non-existent, so you are assigning null to your variable.

    It is also true that you shouldn't depend on being able to set the .value of a <select>.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Code:
    	var randomNumber = Math.floor((Math.random()*5)+4);
    	
    	var runme = function(){
    		document.getElementById('selectBox').value = randomNumber;
    		
    	}
    and add id="selectBox" to the <select>
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    ????

    His code says
    Code:
    <select id='selectBox' name='selectBox'>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    the id is already there, still not sure whats wrong with my code

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i got it to work thanks


  •  

    Posting Permissions

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