Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  64] [ 5]  / answers: 1 / hits: 15275  / 13 Years ago, fri, november 18, 2011, 12:00:00

Is there a better command than this to check if a form is exists and if so submit it? Right now I'm using:



if(document.forms[0])document.forms[0].submit()


Which works, however if form[0] does not exist it throws an exception. I'm trying to keep this dynamic in the sense I don't know what the form ID or name is.


More From » javascript

 Answers
25

To avoid the exception you could do:



if (typeof document.forms[0] !== 'undefined') document.forms[0].submit();

[#89046] Wednesday, November 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katieh

Total Points: 692
Total Questions: 104
Total Answers: 104

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
;