P
Pete
I've been creating a page that when an anchor (from a list of names)
is clicked on, it sets that name in a form to be sent as a search to
Google when a form button is clicked. [I'm testing with Firefox 2.0.0.5.
Don't have access on my non-windows box to IE etc...]
Initially, I tried this scheme (abbreviated here, but I've checked the
behaviour with this too):
The <body> has a form element like this:
<form class="googler" name="googlesearch" id="GOOGFORM"
action="junk.html/search" target="google">
<input type=hidden name=btnG value="Google Search">
<input type="hidden" id="GOOGLE" name=q title="Google Search" value="test string">
<input type="submit" value="Google">
</form>
(I'm using a local file rather than google.com so I just see the sent
request)
Then in the <script>:
function setGoogle(elem) {
var qbtn = document.getElementById("GOOGLE")
var form = document.getElementById("GOOGFORM")
qbtn.value = <<string extracted from elem>>;
...
}
This would work as expected the *first* time setGoogle was called,
but the *second* time, the value in the form that was actually sent
was still the *first* value! Then I'd do it again, and it would
work properly; then the third time it would fail, and so on...
Various other odd things showed up, like the 'GOOGLE'-id element
apparently not having an associated form, or vice versa.
Then I read the faq and found a recommended method, like this:
function setGoogle(elem) {
var form = document.forms["googlesearch"];
var qbtn = form.elements["q"];
qbtn.value = <<string extracted from elem>>;
}
This works as it should, so I'm finally where I want to be, but
*why* should the first idea *not* work? I thought the point of ids
were that they would find the unique element with that id. There's
nothing dynamically created here (and I made sure it was unique!)
so things are definitely not as *I* expected!
Hellppp....
-- Pete --
is clicked on, it sets that name in a form to be sent as a search to
Google when a form button is clicked. [I'm testing with Firefox 2.0.0.5.
Don't have access on my non-windows box to IE etc...]
Initially, I tried this scheme (abbreviated here, but I've checked the
behaviour with this too):
The <body> has a form element like this:
<form class="googler" name="googlesearch" id="GOOGFORM"
action="junk.html/search" target="google">
<input type=hidden name=btnG value="Google Search">
<input type="hidden" id="GOOGLE" name=q title="Google Search" value="test string">
<input type="submit" value="Google">
</form>
(I'm using a local file rather than google.com so I just see the sent
request)
Then in the <script>:
function setGoogle(elem) {
var qbtn = document.getElementById("GOOGLE")
var form = document.getElementById("GOOGFORM")
qbtn.value = <<string extracted from elem>>;
...
}
This would work as expected the *first* time setGoogle was called,
but the *second* time, the value in the form that was actually sent
was still the *first* value! Then I'd do it again, and it would
work properly; then the third time it would fail, and so on...
Various other odd things showed up, like the 'GOOGLE'-id element
apparently not having an associated form, or vice versa.
Then I read the faq and found a recommended method, like this:
function setGoogle(elem) {
var form = document.forms["googlesearch"];
var qbtn = form.elements["q"];
qbtn.value = <<string extracted from elem>>;
}
This works as it should, so I'm finally where I want to be, but
*why* should the first idea *not* work? I thought the point of ids
were that they would find the unique element with that id. There's
nothing dynamically created here (and I made sure it was unique!)
so things are definitely not as *I* expected!
Hellppp....
-- Pete --