View Full Version : Error I Can't Figure Out, Help!

06-22-2009, 02:59 AM
I'm sure that one of you will be able to glance at this and tell me what's wrong with it.

I have a search engine javascript that worked just fine when I had it in a dropdown box. When I switched it to radio buttons, it quit working, and nothing I have tried has fixed it. Here's the working script:

<script type="text/javascript">
function dosearch() {
var sf=document.searchform;
var submitto = sf.sengines.options[sf.sengines.selectedIndex].value + escape(sf.searchterms.value);
window.location.href = submitto;
return false;

<form name="searchform" onSubmit="return dosearch();">

<select name="sengines">
<option value="http://www.google.com/search?q=" selected>Google</option>
<option value="http://www.altavista.com/web/results?q=">Alta Vista</option>
<option value="http://www.dogpile.com/info.dogpl/search/web/">Dogpile</option>
<option value="http://search.yahoo.com/search?p=">Yahoo!</option>
<option value="http://www.bing.com/search?q=">Bing</option>

<input type="text" name="searchterms">
<input type="submit" name="SearchSubmit" value="Search">

And here's what I want the form to look like:

<input type="radio" name="sengines" value="http://www.google.com/search?q=">
<img src="google_icon.gif">
<input type="radio" name="sengines" value="http://www.altavista.com/web/results?q=">
<img src="altavista_icon.gif">
<input type="radio" name="sengines" value="http://www.dogpile.com/info.dogpl/search/web/">
<img src="dogpile_icon.gif">
<input type="radio" name="sengines" value="http://search.yahoo.com/search?p=">
<img src="yahoo_icon.gif">
<input type="radio" name="sengines" value="http://www.bing.com/search?q=">
<img src="bing_icon.gif">

Unfortunately, changing the form to radio buttons seems to break. I know it's something easy I'm missing...Help!

06-22-2009, 04:15 AM
Radio button groups don't have a selectedIndex property, you must scan for a selected button. This may work:

function dosearch()
var sf = document.forms.searchform, sfe = sf.engines;

for( var i = 0, len = sfe.length; i < len && !sfe[ i ].checked; i++ )

if ( i != len )
var submitto = sfe[ i ].value + escape(sf.searchterms.value);

window.location.href = submitto;

return false;

06-22-2009, 07:08 AM
Indeed it does, with the change from "engines" to "sengines." :) Thank you so much!