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
Simple Code..Variable Showing Undefined..Why?
Old 12-16-2008, 11:24 AM Simple Code..Variable Showing Undefined..Why?
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Hello.

Can anyone tell me why in this simple code my variable "the_values" is showing undefined in Firebug? or better yet..why it's not doing anything...

Code:
<html><head><title>Whatever</title>
<script type="text/javascript">

function attachHandlers(){
var the_button = document.getElementById("the_click");
the_button.onclick=showPicks();
}

function showPicks(){
var the_values = document.getElementsByName("the_numbers").value;

var my_selections = new Array();

for (var i=0; i<the_values.length; i++){
my_selections= the_values[i];
}

alert(my_selections);

}
</script></head>

<body onload="attachHandlers()">
<form>
<input type="textbox" name="the_numbers" size="1" value="1">
<input type="textbox" name="the_numbers" size="1" value="2">
<input type="textbox" name="the_numbers" size="1" value="3">
<input type="textbox" name="the_numbers" size="1" value="4"><br>
<input type="button" value="checknumbers" size="1" id="the_click">
</form>
</body>
</html>
LayneMitch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-16-2008, 12:06 PM Re: Simple Code..Variable Showing Undefined..Why?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
This line is meaningless:
Code:
var the_values = document.getElementsByName("the_numbers").value;
Since you can't access the value attribute this way. getElementsByName creates an array of objects with "the_numbers" as a name attribute. Each of these objects will have attributes such as value attached to them, but to access them, you must loop over the array. So the above line becomes this:
Code:
var the_values = document.getElementsByName("the_numbers");
For another thing, the line my_selections=the_values[i] is probably not doing what you expect it to, since you defined my_selections as an array right above there. To add an element to the next item in an array, use the array's push method:
Code:
my_selections.push(the_values[i].value);
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 12-17-2008, 10:17 AM Re: Simple Code..Variable Showing Undefined..Why?
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
This line is meaningless:
Code:
var the_values = document.getElementsByName("the_numbers").value;
Since you can't access the value attribute this way. getElementsByName creates an array of objects with "the_numbers" as a name attribute. Each of these objects will have attributes such as value attached to them, but to access them, you must loop over the array. So the above line becomes this:
Code:
var the_values = document.getElementsByName("the_numbers");
For another thing, the line my_selections=the_values[i] is probably not doing what you expect it to, since you defined my_selections as an array right above there. To add an element to the next item in an array, use the array's push method:
Code:
my_selections.push(the_values[i].value);
Got it...I wasn't thinking about that array and how I would have to loop through it first...also I completely forgot about the push() method. Thanks.
LayneMitch is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Simple Code..Variable Showing Undefined..Why?
 

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.13546 seconds with 12 queries