• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

onclick as a replacement for onsubmit

 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Is it possible to register onclick and avoid using onsubmit. (Then if submission is needed i could call document.forms[0].submit())

Within the body tag.


Within script file:


I get the below error:
"document.forms[0] is undefined"

Thanks,
John Eipe
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That way of addressing elements is old, antiquated, imprecise, and fragile. Give the elements to be addressed id values and use document.getElementById() to fetch references to them.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sheriff,

I was just trying code for practise. (I'm just a beginner). Is it not working because getElementsByTagName is not supported by all browsers?

Thanks,
John Eipe
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is something wrong with document.forms[0]?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Eipe wrote:Is something wrong with document.forms[0]?


Bear Bibeault wrote:That way of addressing elements is old, antiquated, imprecise, and fragile.


 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Eipe wrote:Is something wrong with document.forms[0]?


Not that I can see. The thing is most of us haven't used that format in so long, we are unlikely to catch the error. Using getElementById() is a good habit to get into. And since your code isn't working, you have to change something anyway. Might as well do so now.

I don't think that is the problem though. I think this line is wrong:


The parens tell JavaScript to execute initAll() when you run that line. At this point, the form doesn't exist yet. Whereas without the parens, it sets window.onload to be a pointer to the function to run later.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

John Eipe wrote:Is something wrong with document.forms[0]?


Not that I can see.


I'll respectfully disagree.

It's inherently obtuse, imprecise and unclear what's actually being addressed, and fragile because a re-arrangement of the structure of the markup can cause a regression that's hard to find.

Far better to explicitly id the element and address it by that id. Rick-solid, easily inspect-able, and immune from breaking because something else changed on the page.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Jeanne Boyarsky wrote:

John Eipe wrote:Is something wrong with document.forms[0]?


Not that I can see.


I'll respectfully disagree.


I think we are using the word "wrong" differently. I meant that it should work not that it is a good way of doing things. For example: <%+ abc %> is wrong but <%= "abc" %> is not wrong. It's not good, but it's not wrong in my definition.

I think it is much more significant that window.onload = initAll(); is wrong unless I'm mistaken. As in "it doesn't do what the author intended."
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No argument there -- I just wanted to emphasize that frequently "the wrong" of a problem can be traced to using the imprecise notation format, and that eliminating it as a possible factor in the problem is always a good first step. And also to make sure that later viewers of this post don't think that the antiquated notation is a good way of coding.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As strongly emphasised I write programs only using getElementsById. This is my second program were I used getElementsByTagName and it is just for learning purpose.

Pasted below is the corrected code. (There were few other errors like - visibility of inputelement)




Thanks,
John Eipe
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic