Page 1 of 4 123 ... LastLast
Results 1 to 10 of 39

Thread: Two events in one -

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Two events in one -

    Hello all ,

    I have seen some sites long ago which had a button which executed a script onclick, but these buttons when clicked again did a different job then before and that is something that wish to know on how to do now.

    An example would be - there is a button click it once the background is yellow, click it again the background is blue, click it again the background is gray nad click it again it is back to yellow.

    So simply i just want to know how to assign more than one kind onclick event to one button...

    Please help me

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    HTML:

    HTML Code:
    <form name="form">
    <input type="button" onClick="dosomestuff()" name="button">
    </form>
    JavaScript: //ahh!! won't let me fix the capitalization!

    Code:
    <script type="text/javascript">
    function dosomestuff() {
    
    // Do whatever stuff you want here
    
    document.forms['form'].elements['button'].setAttribute("onClick","someotherfunction()")
    
    }
    </script>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You lost me completely

    What do you mean???

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Have you read the comments?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    By comments do you mean the first post you made in this topic???
    Otherwise i have got no idea on what you are talking about...

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    function rotate_bg(){
    if(!rotate_bg.ct)
    rotate_bg.ct=1
    document.body.style.backgroundColor=rotate_bg.ct==1? 'yellow' : rotate_bg.ct==2? 'blue' : 'gray';
    rotate_bg.ct=rotate_bg.ct++<3? rotate_bg.ct : 0;
    }
    </script>
    HTML Code:
    <input type="button" onclick="rotate_bg();" value="Chng BG">
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reply but i am completely confused -
    Please explain your script

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    document.forms['form'].elements['button'].setAttribute("onClick","someotherfunction()")
    An excellent example of how not to set an event (or name elements)
    rotate_bg.ct=rotate_bg.ct++<3? rotate_bg.ct : 0;
    Note here the difference between ++rotate_bg.ct and rotate_bg.ct++: the fact that you've used the latter here means that rotate_bg.ct will reach 3 before being reset.

    Personally, I would do it like this:
    Code:
    <script type="text/javascript">
      function rotateBackground() {
        return document.body.style.backgroundColor =
          rotateBackground.backgrounds[
            ++rotateBackground.current &#37; 
            rotateBackground.backgrounds.length
          ];
      }
      rotateBackground.current = 0;
      rotateBackground.backgrounds = [
        'yellow',
        'red',
        'green',
        'blue'
      ];
    </script>
    <input type="button" onclick="rotateBackground(); return false;" value="Change background">
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, I cleaned it up a little bit. Here is that version, commented and as part of a demo page:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function rotate_bg(){
    if(!rotate_bg.ct)  //Is counter not initialized?
    rotate_bg.ct=0;  //Initialize and set it to 0 if not.
    //Below we set the page's background color to yellow if counter=0, to blue if counter=1, otherwise to gray.
    document.body.style.backgroundColor=rotate_bg.ct==0? 'yellow' : rotate_bg.ct==1? 'blue' : 'gray';
    //Below we increment the counter's value by one unless it already has reached 2, in which case we set it back to 0.
    rotate_bg.ct=rotate_bg.ct++<2? rotate_bg.ct : 0;
    }
    </script>
    </head>
    <body>
    <input type="button" onclick="rotate_bg();" value="Chng BG"><br>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    (raises an eyebrow)
    if(!rotate_bg.ct) //Is counter not initialized?
    rotate_bg.ct=0; //Initialize and set it to 0 if not.
    Why not just initialise it outside the function?
    //Below we set the page's background color to yellow if counter=0, to blue if counter=1, otherwise to gray.
    document.body.style.backgroundColor=rotate_bg.ct==0? 'yellow' : rotate_bg.ct==1? 'blue' : 'gray';
    I don't usually agree with people when they say this, but you've proved me wrong in one case anyway: nested without proper bracketing and indentation, at least, the conditional operator is virtually unreadable
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •