Click to See Complete Forum and Search --> : Problem with Form id instead of name


Brutality
08-28-2003, 05:56 PM
Recently I updated our homepage to xhtml 1 strict. However, this document type requires forms to use ID's instead of names, and as a result has caused a problem with one script running on the site.

Script in question can be found at the top (WWW search script)
http://www.cwc.co.nz/scripts/global.js
and is obviously related to the search that resides top left on the site http://www.cwc.co.nz

The basic problem is this - the script originally uses document.search.searchtext.value to acess the value entered, but of course this requires the form to have a name. I removed the 'document' part, leaving
search.searchtext.value, and this works in IE and Opera, but not Mozilla and NS, so this is unacceptable to me (but not my boss)

The question. Is there anyway of using formobj.form[] or some other method to access the form without needing a name?

Heres the script

/////////////////////
//WWW SEARCH FUNCTION
/////////////////////

function startSearch(){
searchString = document.search.searchtext.value;

if(searchString != ""){
searchEngine = document.search.whichEngine.selectedIndex + 1;
finalSearchString = "";

if(searchEngine == 1){
finalSearchString = "http://www.google.co.nz/search?q=" + searchString + "&hl=en&lr=";
}
if(searchEngine == 2){
finalSearchString = "http://www.altavista.digital.com/cgi-bin/query?pg=q&what=web&fmt=.&q=" + searchString;
}
if(searchEngine == 3){
finalSearchString = "http://av.yahoo.com/bin/query?p=" + searchString + "&hc=0&hs=0";
}
if(searchEngine == 4){
finalSearchString = "http://www.hotbot.com/?SW=web&SM=MC&MT=" + searchString + "&DC=10&DE=2&RG=NA&_v=2&act.search.x=89&act.search.y=7";
}
if(searchEngine == 5){
finalSearchString = "http://www.lycos.com/cgi-bin/pursuit?adv=%26adv%3B&cat=lycos&matchmode=and&query=" + searchString + "&x=45&y=11";
}
if(searchEngine == 6){
finalSearchString = "http://www.searchnz.co.nz/cgi-bin/search?q=" + searchString + "&fm=&gr=";
}
if(searchEngine == 7){
finalSearchString = "http://www.yellowpages.co.nz/quick/search?key=" + searchString + "&lkey=&stype=&search.x=0&search.y=0";
}
if(searchEngine == 8){
finalSearchString = "http://www.cwc.co.nz/cgi-bin/ksearch/ksearch.pl?searchtext=" + searchString + "&all=1&c=0&w=s&st=s&sort=Matches&display=5&b=1&t=1&u=1&alt=1&l=1&default=1&d=1&k=1&au=1";
}

location.href = finalSearchString;
}
}



All help greatly appreciated.
regards
Mark H

Exuro
08-28-2003, 06:29 PM
You can call the form by it's number. For example, form[0] is the first form on the page, form[1] is the second, ect.

But, if I were you I'd just give everything an ID. This way, you can get the values of the form fields just by using their ID instead of going through the form. For example:

searchString = document.search.searchtext.value

Would change to:

searchString = document.getElementById('searchtext').value

Of course, you'd have to put change the name to an id for each element for that to work:

<input id="searchtext" type="text" value="" size="8" style="font-size:1.2em;" />

Anyway, I hope one of those ways works for you!

Brutality
08-28-2003, 06:45 PM
Fantastic..yes, with a little modification on that field and some others in the script also, it now works. Thank you Exuro :-)