SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    On click => excecute 2 javascript functions

    Hello,

    I have a form with 2 submit buttons, "Test" and "Save".

    I also have to javascript functions, "TestImage" and "SaveImage".

    What I want to do is, when the "Test" button get's clicked, then execute the "TestImage" function. That was easy, I did it like this:

    PHP Code:
    <input type="submit" name="test" value="Test" onClick="return TestImage(this);"
    But what I also want is, when someone clicks on the "Save" button, to first execute the "TestImage" function, and then the "SaveImage" function. Anyone know how I can do this?
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code HTML4Strict:
    <input type="submit" name="test" value="Test" onclick="return TestImage(this) &amp;&amp; SaveImage(this);">
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    Code HTML4Strict:
    <input type="submit" name="test" value="Test" onclick="return TestImage(this) &amp;&amp; SaveImage(this);">
    This doesn't seem to work. Neither does this:

    HTML Code:
    <input type="submit" name="test" value="Test" onclick="return TestImage(this); return SaveImage(this);">
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Are both functions supposed to return true or false? If so then Tommy's code will run TestImage and if that returns true will then run SaveImage. If that also returns true then the form will be submitted. If it isn't doing that then you have a typo in your code.

    Your second method would run TestImage and if that returns true would then submit the form. The SaveImage call would never be reached because the statement before it is returning a value to the click event.
    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="^$">

  5. #5
    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)
    Quote Originally Posted by DeNasio View Post
    This doesn't seem to work. Neither does this:

    HTML Code:
    <input type="submit" name="test" value="Test" onclick="return TestImage(this); return SaveImage(this);">
    I would suggest creating a third function to perform this work for you.

    Code JavaScript:
    function TestAndSaveImage(this) {
      if (TestImage(this)) {
        return SaveImage(this);
      }
      return false;
    }

    And then call it with

    HTML Code:
    <input type="submit" name="test" value="Test" onclick="return TestAndSaveImage(this);">

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @pmw57: Why would that be better than what I suggested, which does exactly the same thing without the need for a third function?

    @DeNasio: Does the SaveImage() function return a boolean value? It should return true for the submit to go through, or false to prevent form submission (provided TestImage() returns true in the first place).
    Birnam wood is come to Dunsinane

  7. #7
    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)
    Quote Originally Posted by AutisticCuckoo View Post
    @pmw57: Why would that be better than what I suggested, which does exactly the same thing without the need for a third function?
    I personally like your solution, however I'm wanting to provide a framework that will allow easier testing while he's troubleshooting his issues, after which when it's behaving properly it can be simplified to function1() && function2()

  8. #8
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Are both functions supposed to return true or false? If so then Tommy's code will run TestImage and if that returns true will then run SaveImage. If that also returns true then the form will be submitted. If it isn't doing that then you have a typo in your code.

    Your second method would run TestImage and if that returns true would then submit the form. The SaveImage call would never be reached because the statement before it is returning a value to the click event.
    Like I said, the code is not working. TestImage returns true or false, but SaveImage uses opener.document.form['field'].value = image; to write the image URL to the form and then closes the popup window with window.close();
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor


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
  •