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
Can't check for an element without causing an error
Old 01-21-2009, 05:34 AM Can't check for an element without causing an error
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
I'm using the following script to select the first text input field of the first form, however I can't test for the element without causing an error!

Code:
function firstSelect ()
    {
        if (document.forms[0].getElementsByTagName ('input'))
            {
                var input = document.forms[0].getElementsByTagName ('input');
        
                // get first text input
                for (i = 0; i < input.length; i ++)
                    {
                        if (input[i].type == 'text')
                            {
                                input[i].focus ();
                                break; }
                    }
            }
    }

window.onload = firstSelect;
I assumed that using if (document.forms[0].getElementsByTagName ('input')) would prevent this, but it doesn't seem to?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 01-21-2009, 06:07 AM Re: Can't check for an element without causing an error
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I think getElementsByTagName always returns an array, even when it found nothing.
And maybe an empty array counts as true, so you should check the length of the array instead?
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-21-2009, 08:00 AM Re: Can't check for an element without causing an error
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Hmmm I've tried it with the following:

Code:
if (document.forms[0].getElementsByTagName ('input').length > 0)
But still no luck

The error I'm getting is

Quote:
Error: document.forms[0] is undefined
Source File: http://www.pealo.local/xampp/mattPea...FirstSelect.js
Line: 5
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 01-21-2009, 08:03 AM Re: Can't check for an element without causing an error
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Basically that error tells you, there's no form on the page, but you say there is?
Weird!
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-21-2009, 08:04 AM Re: Can't check for an element without causing an error
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Oh no there is no form on the page sorry. I was hoping the script would basically just check to see if there is one, then carry on with the function if it finds a form with an input field in there.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 01-21-2009, 08:08 AM Re: Can't check for an element without causing an error
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Then you should add another check first, I think.
Code:
function firstSelect ()
    {
        if (typeof document.forms[0] != 'undefined')
            {
            if (document.forms[0].getElementsByTagName ('input'))
                {
                    var input = document.forms[0].getElementsByTagName ('input');

                    // get first text input
                    for (i = 0; i < input.length; i ++)
                        {
                            if (input[i].type == 'text')
                                {
                                    input[i].focus ();
                                    break; }
                        }
                }
            }
    }

window.onload = firstSelect;
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-21-2009, 08:17 AM Re: Can't check for an element without causing an error
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Success! I wish I'd have thought of that lol.
Thanks for the help.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to Can't check for an element without causing an error
 

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