Results 1 to 3 of 3

Thread: Radio and Button Onclick

  1. #1
    Join Date
    Apr 2006
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Radio and Button Onclick

    Hi I've been trying to do this for like an hour but I couldn't do it. I'm a javascript noob. I would really appreciate your help.

    This is the code:

    <form name="download">
    <input type="radio" name="file" value="image1.zip">Image 1
    <input type="radio" name="file" value="image2.zip">Image 2
    <input type="radio" name="file" value="image3.zip">Image 3
    <input type="button" onclick="location=document.download.file.input[document.download.file.checked].value" value="Download">
    </form>

    This idea is that I want to select a filename from radio input and when I'll click that button, the file will be downloaded. I did it with select and options but radio input seems quite difficult for me. I hope you can help.

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by fr600
    <form name="download">
    <input type="radio" name="file" value="image1.zip">Image 1
    <input type="radio" name="file" value="image2.zip">Image 2
    <input type="radio" name="file" value="image3.zip">Image 3
    <input type="button" onclick="location=document.download.file.input[document.download.file.checked].value" value="Download">
    </form>

    This idea is that I want to select a filename from radio input and when I'll click that button, the file will be downloaded.
    There is absolutely no value in doing this. Not only will it fail for users without scripting support, but it necessitates two actions, rather than just one. Use links and be done with it.

    As for the code failure, you might want to read about referencing forms and form controls.

    Mike

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

    Default

    Better to do it this way, though using Javascript for this purpose is far from optimal:
    Code:
    <form name="download" action="" method="post" onsubmit="
      var e = this.elements['file'];
      for(var i=0;i<e.length;i++) if(e[i].checked) window.location.href = e[i].value;
    ">
      <label><input type="radio" name="file" value="image1.zip">Image 1</label>
      <label><input type="radio" name="file" value="image2.zip">Image 2</label>
      <label><input type="radio" name="file" value="image3.zip">Image 3</label>
      <input type="submit" value="Download">
    </form>
    /EDIT: Ah, Mike beat me to it.
    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
  •