homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

"variable has no properties" error
getElementById dynamic array

 5:14 pm on Nov 22, 2005 (gmt 0)

Can't seem to resolve this problem here. I need to know if a particular button (a variable actually) exists by going through a prepopulated comma separated list of numeric values.
I'm trying to access the properties (e.g. src and value) of an input type image via getElementById using a comma separated string converted into an array.
Below is the part of my code:


<input type="image" src="btn_a.gif" name="btn_1" id="btn_1" />
<input type="image" src="btn_a.gif" name="btn_2" id="btn_2" />
<input type="hidden" name="num" id="num" value="1,2,3" />
<img src="spcr.gif" onload="check()" />

my JavaScript

function check(){
var r_num = document.getElementById('num');
var r_num_arr = new Array();
r_num_arr = r_num.value.split(',');

for ( var i = 0; i < r_num_arr.length; i++ ){
var num = r_num_arr[i];
var button = document.getElementById('btn_' + num);
button.src = btn_b.gif';

The interesting thing is that if I use the alert it works - I still get the error "button has no properties" but it works. If i comment out the alert it doesn't work - but i still get the error.

Anyone understand what's going on here?
Please help!



 5:21 pm on Nov 22, 2005 (gmt 0)

Forgot to mention (perhaps obvious):
as a result of the code each button image (or rsc) should be changed from "btn_a.gif" to "btn_b.gif" if the button name (concatenated adding the variable num) is in the variable r_num (or hidden field "num").

Also just restating (perhaps irrelevant): the hidden field "num" is not prepopulated - it's populated on runtime.


 5:33 pm on Nov 22, 2005 (gmt 0)

Just a slight modification:

var button = document.getElementById('btn_' + num);
button.src = btn_b.gif';

Change to:

var button = document.getElementById('btn_' + num);
if( button ) button.src = btn_b.gif';


 6:54 pm on Nov 22, 2005 (gmt 0)

Thank you!
Just as I kept testing with it I realised that if the button does not actually exist then of course it has no properties (dough!)

Thanks again for your help!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved