Lasse Reichstein Nielsen wrote:
> Mattias Campe <> writes:
>>On http://student.ugent.be/astrid/bewoners.php I got the problem that
>>I want Javascript to let my browser go to
>>http://student.ugent.be/astrid/bewon...ginAcjaar=2002 when I
>>select 2002-03 in the form in the upper right corner (idem dito for
>>the other years).
>
>
> I see two problems.
>
>
>>My form is composed by:
>><form method="post" action="bewoners.php">
>> <select onchange="veranderAcjaar()" name="selecteerAcjaar">
>> <option>2001-02</option>
>
> ...
>
>>and my Javascript code (that I've placed in <head/>):
>><script language="Javascript" type="text/javascript">
>> function veranderAcjaar() {
>> var acjaar =
>> selecteerAcjaar.options[selecteerAcjaar.selectedIndex].value
>
>
> Problem one: You haven't declared the variable "selecteerAcjaar".
> Some browsers (most notably IE) automatically declare global variables
> with the same name as named elements on the page. Other browsers
> don't.
Thanks for pointing me on this one, I also like my code most when it
doesn't have to presume anything about the used browser...
> Change this to
> var selRef = document.forms[0].elements['selecteerAcjaar'];
> var acjaar = selRef.options[selRef.selectedIndex].text;
> (or, preferably, pass the selRef as an argument to the function as
> onchange="veranderAcjaar(this);" )
Do you mean that I could just use the following with veranderAcjaar(this):
var acjaar = this.options[selRef.selectedIndex].text;
?
> Problem two: notice the ".text" at the end instead of ".value". The
> content of an option is the "text" property, not the "value"
> property. The value is specified as
> <option value="Value">text</option>
ic
>> location =
>> "http://student.ugent.be/astrid/bewoners.php?beginAcjaar=" +
>> acjaar
>
>
> I prefer
> location.href = ...
done
> And remember the semicolon at the end.
Indeed, forgot about it :-s
>>What could be wrong with this code? It /seems/ right to me, but
>>apparently it isn't. The browser doesn't even seem to execute the
>>function :-s (hmm, my knowledge of Javascript doesn't seem that good
>>:-/)
>
>
> Nor your knowledge of how to make browsers show error messages:
> <URL:http://jibbering.com/faq/#FAQ4_43>
Wauw, thanks for all the help, that link is bookmarked

! But,
unfortunately, it still doesn't work. When I use the Javascript console
from Firefox, it tells me:
Error: selRef has no properties
Source file:
http://student.ugent.be/astrid/bewoners.php Line:25
I checked the faq you gave me and
http://jibbering.com/faq/#FAQ4_13 uses
quiet the same syntax that you gave me, so I don't see a problem there.
I hope you could help me again a little bit further

.
For the completeness, here's the new code:
<script language="Javascript" type="text/javascript">
function veranderAcjaar() {
var selRef = document.forms[0].elements['selecteerAcjaar'];
var acjaar = selRef.options[selRef.selectedIndex].text;
location.href =
"http://student.ugent.be/astrid/bewoners.php?beginAcjaar=" + acjaar;
}
</script>
and:
<form method="post" action="bewoners.php">
<select onchange="veranderAcjaar()" name="selecteerAcjaar">
<option>2001-02</option>
<option>2002-03</option>
<option>2003-04</option>
</select>
</form>
Greetings,
Mattias