Error: document.getElementsByName(userid)[0] has no properties

D

david.kuczek

I got the following error in a javascript I wrote. The script works
fine, but why is the error being displayed???

##### Here comes the little script:

function chgMusicLink(userid,userlink,usertype) {
var card_music_id = document.getElementsByName(userid)[0].value;

document.getElementsByName(userlink)[0].href="/card_music_preview.tcl?card_music_id="+card_music_id+"&card_music_type="+usertype;
}

#####

Thanks
 
S

sleepinglord

alert(document.getElementsByName(userid)).

Check if you really got an object. I guess not.
 
R

Randy Webb

(e-mail address removed) said the following on 5/8/2006 5:44 AM:
I got the following error in a javascript I wrote. The script works
fine, but why is the error being displayed???

Evidently it doesn't "work fine" or you wouldn't get the error.
You get the error because there is an error in your code.

What is the error message?

Your problem probably lies in the area of you passing the function a
string parameter to try to access an object that doesn't exist.
 
T

tim.n.hunt

I got the following error in a javascript I wrote. The script works
fine, but why is the error being displayed???

##### Here comes the little script:

function chgMusicLink(userid,userlink,usertype) {
var card_music_id = document.getElementsByName(userid)[0].value;

document.getElementsByName(userlink)[0].href="/card_music_preview.tcl?card_music_id="+card_music_id+"&card_music_type="+usertype;
}

#####

Thanks

Does the error appear in IE but not Firefox?

If this is the case then it is because IE does not recognise the name
attributes in most tags, for example getElementsByName will return an
empty list when the only matching name attributes are in divs.

The explanation I plagiarised and the list of tags IE recognises is
here
http://jszen.blogspot.com/2004/07/whats-in-name.html

A possible drawback to relying on getElementsByName is that the name
attribute has been deprecated in XHTML1.0 and the w3c plan to remove
it altogether, they have already replaced the name attribute with id
for <a> and <map> in the current XHTML1.1 proposal.

A solution is to use a different attribute, (most likely id or
classname, would consider title if I get desperate), then use a
function that walks over the dom or a nodelist (if you use
getElementsByTagname to reduce the number of nodes searched) to find a
match.

You could do this yourself but there are plenty of pre-written versions
that do this, unfortunately I can't remember where any are.

Hope this helps

Tim
 
T

Thomas 'PointedEars' Lahn

I got the following error in a javascript I wrote. The script works
fine, but why is the error being displayed???

Because the code is junk^W error-prone.
##### Here comes the little script:

function chgMusicLink(userid,userlink,usertype) {
var card_music_id = document.getElementsByName(userid)[0].value;

document.getElementsByName(userlink [0].href="/card_music_preview.tcl?card_music_id="+card_music_id+"&card_music_type="+usertype;
}

#####

<URL:http://pointedears.de/scripts/test/whatami#inference>
<URL:http://rfc-editor.org/rfc/rfc3986.txt>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top