PDA

View Full Version : Radio and Button Onclick



fr600
04-23-2006, 03:51 AM
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.

mwinter
04-23-2006, 11:15 AM
<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 (http://www.jibbering.com/faq/faq_notes/form_access.html).

Mike

Twey
04-23-2006, 11:18 AM
Better to do it this way, though using Javascript for this purpose is far from optimal:
<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.