onchange is baffling me...

Discussion in 'Javascript' started by Michelle, Oct 31, 2004.

  1. Michelle

    Michelle Guest

    I have tried every variation of the "onchange" statement below without
    any actual reloading of the page: The FireFox javaScript Console reports
    the following message:

    Error: missing : in conditional expression
    Source Code:
    location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)

    My Code for the dropdown box is:
    <form name='color_schemes' method='post' action='themes.php' >
    Select a scheme:&nbsp;
    <select name='scheme' size='1'
    onchange='location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)'>
    <option value =' 1'>Dark Gray background, Black fonts and Blue/Gray cell
    colors</option>

    <option value =' 2'>Dark Blue background, Black fonts and Blue/Gray cell
    colors</option>
    <option value =' 3'>Gray background, Black and Green fonts and Green
    cell colors</option>
    <option value =' 4'>Light Gray background, Dark Blue fonts and Cyan/Teal
    cell colors</option>
    <option value =' 5'>Light Gray background, Dark Blue fonts and Light
    Blue cell colors</option>
    <option value =' 6'>White background, Black fonts, Outset Borders and
    Red/Rose menu headings</option>
    <option value =' 7'>White background, Black fonts, Outset Borders and
    Blue menu headings</option>
    <option value =' 8'>White background, Black fonts and Blue/Light Gray
    cell colors</option>

    I would appreciate any advice...its probably something that is very
    trivial and a very glaring defect, but I am blind to it.

    Thanks,

    Miki
     
    Michelle, Oct 31, 2004
    #1
    1. Advertising

  2. Michelle

    Lee Guest

    Michelle said:
    >
    >I have tried every variation of the "onchange" statement below without
    >any actual reloading of the page: The FireFox javaScript Console reports
    >the following message:
    >
    >Error: missing : in conditional expression
    >Source Code:
    >location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)
    >
    >My Code for the dropdown box is:
    ><form name='color_schemes' method='post' action='themes.php' >
    >Select a scheme:&nbsp;
    ><select name='scheme' size='1'
    >onchange='location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)'>


    That onchange value is not valid Javascript.
    The right hand side of the (first) assignment operator contains a
    question mark, so it looks like a conditional expression.
    You really meant for it to be a string, so you should use quotes:

    onchange='location.href="themes.php?scheme="+form.color_schemes.options[form.color_schemes.selectedIndex].value'
     
    Lee, Oct 31, 2004
    #2
    1. Advertising

  3. Michelle wrote:

    > I have tried every variation of the "onchange" statement below without
    > any actual reloading of the page: The FireFox javaScript Console reports
    > the following message:
    >
    > Error: missing : in conditional expression
    > Source Code:
    >

    location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)

    It thinks you are trying to do:

    variable = test ? result if true : (result if false)

    For example:

    var foo = document.forms['myForm'].elements['myCheckbox'].checked ?
    "Wonderful" : "Terrible";

    Which is shorthand for:

    var foo;
    if (document.forms['myForm'].elements['myCheckbox'].checked) {
    foo = "Wonderful";
    } else {
    foo = "Terrible";
    }

    Of course, you aren't trying to do anything like that. Strings (like
    "themes.php?scheme=" need to be quoted. To return the combination of a
    string and the contents of a variable, use a +.


    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
     
    David Dorward, Oct 31, 2004
    #3
  4. On Sun, 31 Oct 2004 19:07:33 GMT, Michelle <miki@spam_me.net> wrote:

    [snip]

    > <select name='scheme' size='1'
    > onchange='location.href=themes.php?scheme=
    > (form.color_schemes.options[form.color_schemes.selectedIndex].value)'>


    There are four errors here:

    1) Using a SELECT element to navigate to a different page when the value
    is changed.

    This technique is bad for usability. See:

    <URL:http://groups.google.com/groups?threadm=da7e68e8.0409171356.25f6eca9%40posting.google.com>
    <URL:http://www.cs.tut.fi/~jkorpela/forms/navmenu.html>

    2) Not assigning a string.

    The location.href property is a string, and expects a string to be
    assigned to it.

    themes.php

    is not a string, so it will need to be quoted (along with the rest of the
    literal text). In fact, the script parser will interpret that as the php
    property of the object, themes (which of course, doesn't exist).

    3) An unqualified reference.

    It might not be wise to use

    form.

    to access the SELECT element's form property. Instead, use the this
    operator to refer to the element and then its property:

    this.form.

    However, that should suggest that referencing the form property is
    unnecessary, anyway.

    this.options[this.selectedIndex].value

    will do.

    4) Accessing a non-existent control.

    You don't have a form control called color_schemes. That's the name of
    your form.

    Once you combine those together, you'll have valid code. I've moved the
    code into a function for clarity.

    function changeScheme(elem) {
    location.href = 'themes.php?scheme='
    + elem.options[elem.selectedIndex].value;
    }

    <select ... onchange="changeScheme(this);">

    > <option value =' 1'>


    Even if you do go ahead with this approach - using a button instead of
    onchange - you'll have to change these values. You can't have a space in
    the URL.

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Oct 31, 2004
    #4
  5. Michelle <miki@spam_me.net> wrote:

    >I have tried every variation of the "onchange" statement below without
    >any actual reloading of the page: The FireFox javaScript Console reports
    >the following message:
    >
    >Error: missing : in conditional expression
    >Source Code:
    >location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)


    The syntax for the ?: operator is
    (condition) ? (statement) : (statement)
    and is the short form of
    if (condition) { statement } else { statement }

    Your code says to test whether the php property of the themes object
    exists and is non-null/non-zero. If it is then set location.href to
    the value of the dropdown list; if not... oops... there is no : with
    an else clause.

    You want themes.php?scheme= to be a string.

    Regards,
    Steve
     
    Steve van Dongen, Oct 31, 2004
    #5
  6. Michelle

    Michelle Guest

    Lee wrote:
    > Michelle said:
    >
    >>I have tried every variation of the "onchange" statement below without
    >>any actual reloading of the page: The FireFox javaScript Console reports
    >>the following message:
    >>
    >>Error: missing : in conditional expression
    >>Source Code:
    >>location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)
    >>
    >>My Code for the dropdown box is:
    >><form name='color_schemes' method='post' action='themes.php' >
    >>Select a scheme:&nbsp;
    >><select name='scheme' size='1'
    >>onchange='location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)'>

    >
    >
    > That onchange value is not valid Javascript.
    > The right hand side of the (first) assignment operator contains a
    > question mark, so it looks like a conditional expression.
    > You really meant for it to be a string, so you should use quotes:
    >
    > onchange='location.href="themes.php?scheme="+form.color_schemes.options[form.color_schemes.selectedIndex].value'
    >

    Thank you all...such speedy replies...and now it works!!!
     
    Michelle, Oct 31, 2004
    #6
  7. Michelle

    Michelle Guest

    Lee wrote:
    > Michelle said:
    >
    >>I have tried every variation of the "onchange" statement below without
    >>any actual reloading of the page: The FireFox javaScript Console reports
    >>the following message:
    >>
    >>Error: missing : in conditional expression
    >>Source Code:
    >>location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)
    >>
    >>My Code for the dropdown box is:
    >><form name='color_schemes' method='post' action='themes.php' >
    >>Select a scheme:&nbsp;
    >><select name='scheme' size='1'
    >>onchange='location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)'>

    >
    >
    > That onchange value is not valid Javascript.
    > The right hand side of the (first) assignment operator contains a
    > question mark, so it looks like a conditional expression.
    > You really meant for it to be a string, so you should use quotes:
    >
    > onchange='location.href="themes.php?scheme="+form.color_schemes.options[form.color_schemes.selectedIndex].value'
    >

    Thank you all...such speedy replies...and now it works!!!

    But now I will also consider using a button to keep users from having

    fits...

    Miki
     
    Michelle, Oct 31, 2004
    #7
  8. Michelle

    Michelle Guest

    Michelle wrote:
    > I have tried every variation of the "onchange" statement below without
    > any actual reloading of the page: The FireFox javaScript Console reports
    > the following message:
    >
    > Error: missing : in conditional expression
    > Source Code:
    > location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)
    >
    >
    > My Code for the dropdown box is:
    > <form name='color_schemes' method='post' action='themes.php' >
    > Select a scheme:&nbsp;
    > <select name='scheme' size='1'
    > onchange='location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)'>
    >
    > <option value =' 1'>Dark Gray background, Black fonts and Blue/Gray cell
    > colors</option>
    >
    > <option value =' 2'>Dark Blue background, Black fonts and Blue/Gray cell
    > colors</option>
    > <option value =' 3'>Gray background, Black and Green fonts and Green
    > cell colors</option>
    > <option value =' 4'>Light Gray background, Dark Blue fonts and Cyan/Teal
    > cell colors</option>
    > <option value =' 5'>Light Gray background, Dark Blue fonts and Light
    > Blue cell colors</option>
    > <option value =' 6'>White background, Black fonts, Outset Borders and
    > Red/Rose menu headings</option>
    > <option value =' 7'>White background, Black fonts, Outset Borders and
    > Blue menu headings</option>
    > <option value =' 8'>White background, Black fonts and Blue/Light Gray
    > cell colors</option>
    >
    > I would appreciate any advice...its probably something that is very
    > trivial and a very glaring defect, but I am blind to it.
    >
    > Thanks,
    >
    > Miki

    Thank you all...such speedy replies...and now it works!!!

    But now I will also consider using a button to keep users from having

    fits...

    Miki
     
    Michelle, Oct 31, 2004
    #8
  9. Michelle

    Michelle Guest

    Michelle wrote:
    > I have tried every variation of the "onchange" statement below without
    > any actual reloading of the page: The FireFox javaScript Console reports
    > the following message:
    >
    > Error: missing : in conditional expression
    > Source Code:
    > location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)
    >
    >
    > My Code for the dropdown box is:
    > <form name='color_schemes' method='post' action='themes.php' >
    > Select a scheme:&nbsp;
    > <select name='scheme' size='1'
    > onchange='location.href=themes.php?scheme=(form.color_schemes.options[form.color_schemes.selectedIndex].value)'>
    >
    > <option value =' 1'>Dark Gray background, Black fonts and Blue/Gray cell
    > colors</option>
    >
    > <option value =' 2'>Dark Blue background, Black fonts and Blue/Gray cell
    > colors</option>
    > <option value =' 3'>Gray background, Black and Green fonts and Green
    > cell colors</option>
    > <option value =' 4'>Light Gray background, Dark Blue fonts and Cyan/Teal
    > cell colors</option>
    > <option value =' 5'>Light Gray background, Dark Blue fonts and Light
    > Blue cell colors</option>
    > <option value =' 6'>White background, Black fonts, Outset Borders and
    > Red/Rose menu headings</option>
    > <option value =' 7'>White background, Black fonts, Outset Borders and
    > Blue menu headings</option>
    > <option value =' 8'>White background, Black fonts and Blue/Light Gray
    > cell colors</option>
    >
    > I would appreciate any advice...its probably something that is very
    > trivial and a very glaring defect, but I am blind to it.
    >
    > Thanks,
    >
    > Miki

    Thank you all...such speedy replies...and now it works!!!

    But now I will also consider using a button to keep users from having

    fits...

    Miki
     
    Michelle, Oct 31, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Baffling question

    Guest, Nov 8, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    354
    Guest
    Nov 9, 2003
  2. =?Utf-8?B?Q2hhcmxlc0E=?=

    simple but baffling javascript prob

    =?Utf-8?B?Q2hhcmxlc0E=?=, Mar 22, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    5,544
    Gozirra
    Mar 22, 2006
  3. =?Utf-8?B?Q2hhcmxlc0E=?=

    v easy but baffling to me CSS question

    =?Utf-8?B?Q2hhcmxlc0E=?=, Apr 5, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,419
    =?Utf-8?B?Q2hhcmxlc0E=?=
    Apr 5, 2006
  4. Replies:
    1
    Views:
    446
    John C. Bollinger
    May 26, 2005
  5. Swengtoo
    Replies:
    2
    Views:
    431
    tom_usenet
    Feb 6, 2004
Loading...

Share This Page