SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2009
    Location
    new jersey
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onclick returning null. silly n00b question...

    i feel really silly and stupid asking for help with this. i'm hoping that someone who remembers what it was like being a total newbie will help out here.

    the following code is for a simple challenge i've set for myself. i'm building a little bingo game and trying to set up the caller's page (where the random numbers are drawn from). instead of numbers for the cards, i'm using star wars character names. i've not included the array of names, but it is working in the script. the random name shows up as expected when i reload the page.

    my problem is that i'm trying to get a button on the html page to call for a new random name rather than having to reload the whole page. i've tried a couple different things, but i keep getting 'null' in return. i've highlighted the problem code in purple.

    i'm sorry if the problem is is totally obvious to you all, but i just can't see what's wrong here. thanks in advance!

    Code:
    window.onload = greetCaller;
    
    function greetCaller() {
    	document.getElementById("morning").innerHTML = "Hello, Bingo Caller!";
    	document.getElementById("newname").onclick = randomName();
    	randomName();
    }
    
    function randomName() {
    	document.getElementById("newword").innerHTML = randomWord();
    }
    
    function randomWord() {
    	var i = Math.floor(Math.random() * charNames.length);
    	return charNames[i];
    }
    Last edited by littleoracle; Apr 20, 2009 at 08:12.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    onclick is not variable, but event handler so you need to assign function name, not result of function. try this:

    document.getElementById("newname").onclick = randomName;

  3. #3
    SitePoint Member
    Join Date
    Mar 2009
    Location
    new jersey
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that did it! thank you so much!


Bookmarks

Posting Permissions

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