SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple onClick="" actions?

    Hello,

    I am using this code

    Code:
    <form style="display: none;" name="num1form" id="num1form" action="">
    <input type="image" name="1" id="num1" value="1" src="imgl/img_first_01.png" onclick="document.forms[0].action='javascript:ChangeNum1();'"/>
    </form>
    What the above code does is change the action state, it allows me to have multiple image inputs which is great as I can define various inputs with different IDs.

    However, this is used for one section of the page I happen to have another <form> with basically the same code but it's using num2 values, I'm not sure why but I can only using one of these
    Code:
    onclick="document.forms[0].action='javascript:ChangeNum1();'"


    at a time, for one form only.

    Is there anyone able to help me divide this, or make these onClick actions unique so they can be used in more than 1 form?

    Thanks,
    Keenan

  2. #2
    SitePoint Member
    Join Date
    May 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Quote Originally Posted by Keenanm View Post
    Hello,

    I am using this code

    Code:
    <form style="display: none;" name="num1form" id="num1form" action="">
    <input type="image" name="1" id="num1" value="1" src="imgl/img_first_01.png" onclick="document.forms[0].action='javascript:ChangeNum1();'"/>
    </form>
    What the above code does is change the action state, it allows me to have multiple image inputs which is great as I can define various inputs with different IDs.

    However, this is used for one section of the page I happen to have another <form> with basically the same code but it's using num2 values, I'm not sure why but I can only using one of these
    Code:
    onclick="document.forms[0].action='javascript:ChangeNum1();'"


    at a time, for one form only.

    Is there anyone able to help me divide this, or make these onClick actions unique so they can be used in more than 1 form?

    Thanks,
    Keenan


    Here's what you can do:
    - Try writing function into which you will pass the objects you intend to process. Just like this
    function ChangeNum(object){
    //processor codes
    }

    onclick="document.forms[0].action='ChangeNum(object);
    This will prevent redundant coding and mop up your code.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,604
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    You'd do better to set the code in the onsubmit handler and then return false so that the action doesn't run at all. That way you are not trying to stick JavaScript in the action where it doesn't belong.
    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
  •