Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > onchange() doesn't seem to work

Reply
Thread Tools

onchange() doesn't seem to work

 
 
Mattias Campe
Guest
Posts: n/a
 
      04-13-2004
Hello,

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).

My form is composed by:
<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>

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
location =
"http://student.ugent.be/astrid/bewoners.php?beginAcjaar=" + acjaar
}
</script>


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 :-/)



Any help would be appreciated,
greetings,
Mattias
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      04-13-2004
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. 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);" )

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>


> location =
> "http://student.ugent.be/astrid/bewoners.php?beginAcjaar=" +
> acjaar


I prefer
location.href = ...
And remember the semicolon at the end.

> 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>

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Mattias Campe
Guest
Posts: n/a
 
      04-13-2004
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
 
Reply With Quote
 
Morris
Guest
Posts: n/a
 
      04-13-2004

"Mattias Campe" <> a écrit dans le message
de news:c5hcec$d0d$...
> 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;


change the above line to:
var acjaar = selRef.options[selRef.selectedIndex].value;

> 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>


and these to:

<option value=2001>2001-02</option>
<option value=2002>2002-03</option>
<option value=2003>2003-04</option>

> </select>
> </form>
>
> Greetings,
> Mattias



 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      04-13-2004
Mattias Campe <> writes:

> 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>


This code works for me in Opera. Ofcourse it assumes that the relevant
form is the first form on the page (forms[0]). If it isn't, it will fail.
That is why I recommended passing the select as an argument:
---
<script type="text/javascript">
function veranderAcjaar(selRef) {
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(this);" name="selecteerAcjaar">
<option>2001-02</option>
<option>2002-03</option>
<option>2003-04</option>
</select>
</form>
---
(Why the form, btw? You don't use it, since there is no way to submit
it. You can omit the form element entirely when you address the select
directly like this, unless you need the page to work in Netscape 4 too

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Mattias Campe
Guest
Posts: n/a
 
      04-14-2004
Lasse Reichstein Nielsen wrote:
[...]
> This code works for me in Opera. Ofcourse it assumes that the relevant
> form is the first form on the page (forms[0]). If it isn't, it will fail.
> That is why I recommended passing the select as an argument:
> ---
> <script type="text/javascript">
> function veranderAcjaar(selRef) {
> 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(this);" name="selecteerAcjaar">
> <option>2001-02</option>
> <option>2002-03</option>
> <option>2003-04</option>
> </select>
> </form>


I uses (this) and I also used the remark by Morris, because
?beginAcjaar="2001-02" will not work, it has to be ?beginacjaar="2001"
(stupid me :-/ )

> (Why the form, btw? You don't use it, since there is no way to submit
> it. You can omit the form element entirely when you address the select
> directly like this, unless you need the page to work in Netscape 4 too


Well, I thought that a <select> had to be surrounded by <form>, but as
this doesn't seem to be true, I just omitted it.


Really, thanks a lot for the help!
Greetings,
Mattias
 
Reply With Quote
 
Mattias Campe
Guest
Posts: n/a
 
      04-14-2004
Morris wrote:
> "Mattias Campe" <> a écrit dans le message


> change the above line to:
> var acjaar = selRef.options[selRef.selectedIndex].value;
>
>
>> 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>

>
>
> and these to:
>
> <option value=2001>2001-02</option>
> <option value=2002>2002-03</option>
> <option value=2003>2003-04</option>


Of course! How could I overlook that :-/


Really, thanks a lot for the help!
It works now: http://student.ugent.be/astrid/bewoners.php
Greetings,
Mattias
 
Reply With Quote
 
G Roydor
Guest
Posts: n/a
 
      04-14-2004
Donner un nom à votre formulaire (<form name="myform" ....>)
et var acjaar=myform.select..........
GR

Mattias Campe a écrit:
> Hello,
>
> 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).
>
> My form is composed by:
> <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>
>
> 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
> location =
> "http://student.ugent.be/astrid/bewoners.php?beginAcjaar=" + acjaar
> }
> </script>
>
>
> 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 :-/)
>
>
>
> Any help would be appreciated,
> greetings,
> Mattias


 
Reply With Quote
 
Mattias Campe
Guest
Posts: n/a
 
      04-19-2004
G Roydor wrote:
> Donner un nom à votre formulaire (<form name="myform" ....>)
> et var acjaar=myform.select..........


Merci beaucoup, mais j'ai déja une solution ...

> GR
>
> Mattias Campe a écrit:
>
>> Hello,
>>
>> 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).
>>
>> My form is composed by:
>> <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>
>>
>> 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
>> location =
>> "http://student.ugent.be/astrid/bewoners.php?beginAcjaar=" + acjaar
>> }
>> </script>
>>
>>
>> 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 :-/)
>>
>>
>>
>> Any help would be appreciated,
>> greetings,
>> Mattias

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NAT doesn't seem to work on all ports gqmetro@yahoo.com Cisco 1 06-15-2005 08:26 AM
router doesn't seem to work =?Utf-8?B?ZA==?= Wireless Networking 2 06-12-2005 06:45 PM
Can't seem to get debugger to work tfs ASP .Net 1 06-28-2004 09:49 PM
Error downloading page, some pages work great but cant seem to get this one Jack Schafer Perl 1 04-23-2004 08:32 AM
&nbsp; in a ListItem doesn't seem to work Bill Green ASP .Net 2 02-07-2004 01:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57