Page 1 of 1

[SOLVED] passing parameters with javascript functions

Posted: Sat Apr 16, 2005 2:23 am
by psychomachine
Hi.

I am trying to modify a JavaScript code for livesearch functions, (also known as AJAX etc...) which uses XMLHttpRequest. It's all working fine with one search form on the page, but I'm trying to have multiple forms using this on the same page. So, I thought, I should simply pass the name of the form as a parameter, which will then be passed on to the php scripts which will know what to do with it.

Since I am not very experienced with JavaScript, I'm doing something wrong. Originally, this was used for one input box whose name = "q":

Code: Select all

function liveSearchDoSearch() {
liveSearchReq.open(&quote;GET&quote;, liveSearchRoot + &quote;/lsearch?q=&quote; + document.forms.searchform.q.value + liveSearchParams);
}
Which I then changed to:

Code: Select all

function liveSearchDoSearch(which){
liveSearchReq.open(&quote;GET&quote;, liveSearchRoot + &quote;/lsearch?&quote; + which + &quote;=&quote; + document.forms.searchform.q.value + liveSearchParams);
}
Now, I am still passing the value of q as parameter from the web page, i.e. onkeypress = liveSearchDoSearch(q), but the code doesn't work the same anymore. Any idea why?

Furthermore (once the above gets fixed), I will need to change the portion:

Code: Select all

document.forms.searchform.q.value
to replace "q" with whatever parameter was passed. I can already see, however, that simply doing

Code: Select all

document.forms.searchform.which.value
doesn't do the trick.

These are some very basic questions -- I am new to JavaScript, and I'd be very grateful for some hints.

All best,
Tench

Posted: Sat Apr 16, 2005 6:26 am
by feyd
document.forms['formname'].elements['fieldname'].value

Posted: Sat Apr 16, 2005 7:49 am
by psychomachine
thanks a lot feyd, when i replace 'fieldname' with the variable, it works for me. great.