Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
document.forms.elements.value returns undefined second time around
Old 08-13-2009, 02:54 PM document.forms.elements.value returns undefined second time around
Extreme Talker

Posts: 177
Trades: 0
I don't know why but when I use document.forms.elements.value on a set of nodes, the second time around, on the first time, it always returns undefined.

player identifier iterates through like it's supposed to be doing...
Code:
player_total_1
player_total_2
player_total_3
player_total_4
player_total_5
player_total_1
player_total_2
player_total_3
player_total_4
but when it gets to the second for loop....trying to get the player_total_'playerNumber' form element returns undefined and teh script breaks.

This is boggling my mind, thank you for any help.

function that produces the error
Code:
function updatePlayerTotal(playerNumber, cardValue)
{
    
    
    // get player div
    player_identifier = 'player_total_'+playerNumber;

    document.getElementById('response').innerHTML += player_identifier + "<br>";
   HERE-->UNDEFINED--> player_total = document.forms['players'].elements[player_identifier];

    
    new_total = (player_total.value + totalCardValue);
    
    document.getElementById('player_total_display_' + playerNumber).innerHTML = new_total;

    player_total.value = new_total;
    
    
    return;
    
}
calling the function
Code:
for(i = 1; i <= 5; i++)
    {
        
        // next card id
        next_id = document.getElementById('next_card');
        
        // card
        card = document.getElementById('card_' + next_id.value++);
        // count these 4 and return actual value from string
        cardValue = countCards(card);
        //to display the card they get
        cardDisplay = document.getElementById('cards_'+i);
        // not sure why
        parentDiv = document.getElementById('player_'+i);
        // put card in there
        cardDisplay.innerHTML += '<img src="' + card.value + '">';
        count = count+1;
        
        //updatePlayerTotal(playerNumber, cardValue);
        updatePlayerTotal(i, cardValue);
        
        
    }
    
    for(i = 1; i <= 4; i++)
    {
        
        // next card id
        next_id = document.getElementById('next_card');
        // card
        card = document.getElementById('card_' + next_id.value++);
        // count these 4
        cardValue = countCards(card);
        //to display the card they get
        cardDisplay = document.getElementById('cards_'+i);
        // not sure why
        parentDiv = document.getElementById('player_'+i);
        // put card in there
        cardDisplay.innerHTML += '<img src="' + card.value + '">';
        
        //updatePlayerTotal(playerNumber, cardValue);
        updatePlayerTotal(i, cardValue);
    }
and the html
HTML Code:
<form id="players">
<div id="player=1" style="border:1px solid black;width:500px;height:100px;">
    <div id="cards_1" style="float:left;">
    </div>
    <input type="hidden" name="total" value="0">
    <input type="hidden" name="soft" value="0">
    
    <div id="player_total_display_1" style="float:right;height:50px; border-left:1px solid black;width:25px;font-size:32px;">
    0
    <input name="player_total_1" type="hidden" value="0">
    </div>
    <div id="game_options_1" style="display:none;float:right;">
    game me
    </div>
    
    <div style="clear:both;"></div>
</div>
<div id="player=2" style="border:1px solid black;width:500px;height:100px;">
    <div id="cards_2" style="float:left;">
    </div>
    <input type="hidden" name="total" value="0">
    <input type="hidden" name="soft" value="0">
    
    <div id="player_total_display_2" style="float:right;height:50px; border-left:1px solid black;width:25px;font-size:32px;">
    0
    <input name="player_total_2" type="hidden" value="0">
    </div>
    <div id="game_options_2" style="display:none;float:right;">
    game me
    </div>
    
    <div style="clear:both;"></div>
</div>
<div id="player=3" style="border:1px solid black;width:500px;height:100px;">
    <div id="cards_3" style="float:left;">
    </div>
    <input type="hidden" name="total" value="0">
    <input type="hidden" name="soft" value="0">
    
    <div id="player_total_display_3" style="float:right;height:50px; border-left:1px solid black;width:25px;font-size:32px;">
    0
    <input name="player_total_3" type="hidden" value="0">
    </div>
    <div id="game_options_3" style="display:none;float:right;">
    game me
    </div>
    
    <div style="clear:both;"></div>
</div>
<div id="player=4" style="border:1px solid black;width:500px;height:100px;">
    <div id="cards_4" style="float:left;">
    </div>
    <input type="hidden" name="total" value="0">
    <input type="hidden" name="soft" value="0">
    
    <div id="player_total_display_4" style="float:right;height:50px; border-left:1px solid black;width:25px;font-size:32px;">
    0
    <input name="player_total_4" type="hidden" value="0">
    </div>
    <div id="game_options_4" style="display:none;float:right;">
    game me
    </div>
    
    <div style="clear:both;"></div>
</div>
<br><br>
DEALER:<br>
<div id="player=5" style="border:1px solid black;width:500px;height:100px;">
    <div id="cards_5">
    </div>
    <input type="hidden" name="total" value="0">
    <input type="hidden" name="soft" value="0">
    
    <div id="player_total_display_5" style="float:right;height:50px; border-left:1px solid black;width:25px;font-size:32px;">
    0
    <input name="player_total_5" type="hidden" value="0">
    </div>
    
    <input name="dealer_card_two" type="hidden" value="">
    <div style="clear:both;"></div>
</div>
kbfirebreather is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-13-2009, 03:11 PM Re: document.forms.elements.value returns undefined second time around
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
use a different variable name for one of the loops rather than "i" for them both
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-13-2009, 03:16 PM Re: document.forms.elements.value returns undefined second time around
Extreme Talker

Posts: 177
Trades: 0
still returned undefined
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to document.forms.elements.value returns undefined second time around
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11889 seconds with 12 queries