www.webdeveloper.com
Results 1 to 5 of 5

Thread: Script won't work on chrome?

  1. #1
    Join Date
    Feb 2013
    Posts
    9

    Script won't work on chrome?

    Hello!
    I would be grateful if you guys could enlighten me why this script works in other browsers, but fails in chrome.
    It's just supposed to alert a name depending on the fruits you've chosen.

    Code:
    <html>
    <head>
    <title></title>
    
    <script type="text/javascript">
    
    function FruitBox()
    {
       alert(window.document.myform.fruit.value);
    }
    
    </script>
    
    </head>
    <body>
    
    <form name="myform">
    <input type="radio" name="fruit" onClick="window.document.myform.fruit.value = 'oranges'">Oranges & Tangerines<br>
    <input type="radio" name="fruit" onClick="window.document.myform.fruit.value = 'bananas'">Bananas<br>
    <input type="radio" name="fruit" onClick="window.document.myform.fruit.value = 'peaches'">Peaches, Nectarines & Plums<br>
    Choose your favorite and <a href="javascript:FruitBox()">click here</a>.
    </form>
    
    </body>
    </html>

  2. #2
    Join Date
    Feb 2013
    Posts
    9
    Forgot to mention, it returns 'undefined' instead of the fruit value.

  3. #3
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by frrlod View Post
    Forgot to mention, it returns 'undefined' instead of the fruit value.
    The value of a radio button group can be read by looping through the elements (you can research that). There is no need to use onclick events and setting the value of a radio button group is an illegal operation so cannot be expected to work.

    The javascript: pseudo protocol should never be used, you can research why.

    Here's a fix for your code that keeps the onclick events to set a hidden field:
    Code:
    <html>
    <head>
    <title></title>
    
    <script type="text/javascript">
    
    function FruitBox(f)
    {
       alert( f.response.value );
    }
    
    </script>
    
    </head>
    <body>
    
    <form name="myform">
    <input type='hidden' name='response'>
    <input type="radio" name="fruit" onClick="this.form.response.value = 'oranges'">Oranges & Tangerines<br>
    <input type="radio" name="fruit" onClick="this.form.response.value = 'bananas'">Bananas<br>
    <input type="radio" name="fruit" onClick="this.form.response.value = 'peaches'">Peaches, Nectarines & Plums<br>
    <input type="button" value="click here" onclick="FruitBox( this.form )">
    </form>
    
    </body>
    </html>
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  4. #4
    Join Date
    Feb 2013
    Posts
    9
    Thanks a lot.

    I guess learning from a pretty old guide was a bad idea.

  5. #5
    Join Date
    Feb 2013
    Posts
    11
    this is not regularly but some time i always used in script in chrome. i think its work. But i will again try it. Thanks for your brilliant information

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles