Results 1 to 5 of 5

Thread: two different button questions

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default two different button questions

    Code:
    <form name="editform" action="#">
    <img id="img" src="" style="width:250px;" alt="">
    <button onclick="newi1();return false;">prev</button> 
    <button onclick="newi2();return false;">original image</button> 
    <button onclick="newi() ;return false;">next</button> 
    <br><br>
            Image File:
    <br><input type="text" name="outputtext">
    </form>
    
    <script type="text/javascript">
    function newi(){
    if(newi.Num > newi.ar.length - 1){
    newi.Num = 0;
    newi.img.src = '/images/screenshots/' + newi.ar[newi.Num];
    }
    else{
    newi.img.src = '/images/screenshots/' + newi.ar[newi.Num];
    }
    newi.output.value = newi.ar[newi.Num];++newi.Num;return false;
    }
    
    function newi1(){--newi.Num;
    if(newi.Num < 0 ){newi.Num = newi.ar.length - 1;}
    newi.img.src = 'images/screenshots/' + newi.ar[newi.Num];
    newi.output.value = newi.ar[newi.Num];return false;
    }
    
    function newi2(){
    newi.Num = 0;
    newi.img.src = 'images/screenshots/' + newi.ar[newi.Num];
    newi.output.value = newi.ar[newi.Num];return false;
    }
    
    newi.Num = 0;
    newi.ar = new Array(ggit1.jpg,ggit2.jpg,ggit3.jpg,ggit4.jpg,ggit5.jpg);
    newi.img = document.getElementById('img');
    newi.output = document.forms.editform.elements.outputtext;
    newi();
    </script>
    First question: why does the error listed above where the parts of the array are not placed in single quotes cause the buttons to go to an url?

    Second question: when the parts of the array are placed in single quotes everything is fine, but why is it that sometimes you have to push a button twice to get an action? Try pushing the buttons in different orders and you will see what I mean. The only button that always works with only one click is the middle button. http://www.animeviews.com/test/test26.php
    Last edited by jscheuer1; 12-02-2009 at 09:22 AM. Reason: fix broken link
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    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

    1. The form submits.
    2. In newi() you increase the number at the end. In newi1() you decrease the number at the beginning.


    Try:

    Code:
    <form name="editform" action="#">
    <img id="img" src="" style="width:250px;" alt="">
    <button onclick="return newi(-1);">prev</button> 
    <button onclick="return newi();">original image</button> 
    <button onclick="return newi(1);">next</button> 
    <br><br>
            Image File:
    <br><input type="text" name="outputtext">
    </form>
    
    <script type="text/javascript">
    function newi(n){
    	newi.Num += n || -newi.Num;
    	newi.Num = newi.Num < 0? newi.count - 1 : newi.Num % newi.count;
    	newi.img.src = '/images/screenshots/' + newi.ar[newi.Num];
    	newi.output.value = newi.ar[newi.Num];
    	return false;
    }
    
    newi.Num = 0;
    newi.ar = new Array('ggit1.jpg','ggit2.jpg','ggit3.jpg','ggit4.jpg','ggit5.jpg');
    newi.count = newi.ar.length;
    newi.img = document.getElementById('img');
    newi.output = document.forms.editform.elements.outputtext;
    newi();
    </script>
    - John
    ________________________

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

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Nice! I like that trick where you passed a value into the function using n. I never noticed that before because I am so used to variables having a $ before it. It is fun learning something new.

    Your code worked, of course and it is simpler

    I am not sure what you meant by "the form submits." Could you explain that a bit more?
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    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

    Button was a non-standard element that was adopted, by most browsers, I believe even by the W3C standards, due to its widespread use. To be more standard one would use:

    HTML Code:
    <input type="button" value="prev" onclick="whatever();">
    However, even using that syntax, a form with no:

    HTML Code:
    <input type="submit" . . . >
    will submit for when at least one of its buttons is clicked, or upon hitting enter in a text input, which also happens when hitting enter when there is a submit button and only one text input. Using button elements may increase this tendency. Forms submit. That's what they do. Their main use is for server side code that does something with the information submitted. Forms are also required to have an action, yours does, but it is the default action, the one the form would have if no action is defined. The method is not required. GET is the default though. With GET, it (upon submit) launches the the page defined in the action attribute (in this case the same url that is in the address bar), with a query string (for example: ?something=somevalue&anotherthing=anothervalue) appended comprised of all of the named elements in the form and their values, if any.

    By returning false, this is short circuited. Nothing submits. But if there is an error in your script code, it will have undefined or no return value, so the submission occurs.

    So, like I said:

    "The form submits."
    - John
    ________________________

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

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Thank you. That was makes a lot of sense now.
    To choose the lesser of two evils is still to choose evil. My personal site

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
  •