SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)

    Triggering an event

    This recent blog post says that this is how you go about Triggering events in plain JavaScript

    Code Javascript:
     var elem = document.getElementById('myForm');
        var event = new Event('submit');
        elem.dispatchEvent(event);

    Now why is this better than using addEventListener, or the .onsubmit property?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Now why is this better than using addEventListener, or the .onsubmit property?
    From what I can see of how it works you'd use that when you want to trigger the event without any intervention by the person visiting the page.

    For example you could put that code in a function and trigger it using a setTimeout to submit the form after a specific time regardless of whether the person has started filling out the form or is part way through. A simple way to limit the amount of time allowed for entering info in the form because it gets submitted automatically by that code if the person hasn't already triggered the listener or handler directly..
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •