Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > <select..onchange() > with only one <option>

Reply
Thread Tools

<select..onchange() > with only one <option>

 
 
The Natural Philosopher
Guest
Posts: n/a
 
      09-26-2007

This is a nasty one and I can't see my way out of it.

I have a bunch of select statements in a form, and each select statement
has an onchange="do_something(this)" in it, and this works
nicely..except when there is only ONE OPTION in a given select.
It seems you cannot 'onchange' a single option!

Well, that is reasonable.

The trouble is I have no way of selecting it since the form itself is a
dummy: The form action is basically to submit to a new script with
hidden variables carefully set to get the desired action depending on
the last option selected..

onclick() in the <option> itself doesn't work for IE6...

Can anyone think of a way to achieve what I want?


The fragment in question is this one

<TR><TD align="center"><b>Julian's Projects</b></td>
<TD align="center"><select STYLE="width: 200px" name="project_id"
onchange="select_project_id(this)">
<OPTION value= "13" >ME-262 (Sep 13th 07)</OPTION>
</select></TD></TR>

This works stunningly with more than one option..but falls apart with
just one.

The option of making the single option SELECTED is a non starter, as
there are other options in other select statements that deselect this
one..once deselected I can't ever get it back!

I think I tried an onclick() on the whole select thing, but it didn't
work too well.


 
Reply With Quote
 
 
 
 
The Natural Philosopher
Guest
Posts: n/a
 
      09-27-2007
The Natural Philosopher wrote:
>
> This is a nasty one and I can't see my way out of it.
>
> I have a bunch of select statements in a form, and each select statement
> has an onchange="do_something(this)" in it, and this works
> nicely..except when there is only ONE OPTION in a given select.
> It seems you cannot 'onchange' a single option!
>
> Well, that is reasonable.
>
> The trouble is I have no way of selecting it since the form itself is a
> dummy: The form action is basically to submit to a new script with
> hidden variables carefully set to get the desired action depending on
> the last option selected..
>
> onclick() in the <option> itself doesn't work for IE6...
>
> Can anyone think of a way to achieve what I want?
>
>
> The fragment in question is this one
>
> <TR><TD align="center"><b>Julian's Projects</b></td>
> <TD align="center"><select STYLE="width: 200px" name="project_id"
> onchange="select_project_id(this)">
> <OPTION value= "13" >ME-262 (Sep 13th 07)</OPTION>
> </select></TD></TR>
>
> This works stunningly with more than one option..but falls apart with
> just one.
>
> The option of making the single option SELECTED is a non starter, as
> there are other options in other select statements that deselect this
> one..once deselected I can't ever get it back!
>
> I think I tried an onclick() on the whole select thing, but it didn't
> work too well.
>
>


Hmm., I seem to have fixed it by replacing onchange() with onclick()

It seems that onclick(this) picks up the correct value in
this.SelectedIndex..

Oh well. The ways of Javascript passeth man's understanding etc.


 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      09-27-2007
On Sep 26, 7:53 pm, The Natural Philosopher <(E-Mail Removed)> wrote:
> This is a nasty one and I can't see my way out of it.
>
> I have a bunch of select statements in a form, and each select statement
> has an onchange="do_something(this)" in it, and this works
> nicely..except when there is only ONE OPTION in a given select.
> It seems you cannot 'onchange' a single option!
>
> Well, that is reasonable.
>
> The trouble is I have no way of selecting it since the form itself is a
> dummy: The form action is basically to submit to a new script with
> hidden variables carefully set to get the desired action depending on
> the last option selected..
>
> onclick() in the <option> itself doesn't work for IE6...
>
> Can anyone think of a way to achieve what I want?


Why use a select at all if there is only one option? Wouldn't a
hidden input make more sense?

 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      09-27-2007
David Mark wrote:
> On Sep 26, 7:53 pm, The Natural Philosopher <(E-Mail Removed)> wrote:
>> This is a nasty one and I can't see my way out of it.
>>
>> I have a bunch of select statements in a form, and each select statement
>> has an onchange="do_something(this)" in it, and this works
>> nicely..except when there is only ONE OPTION in a given select.
>> It seems you cannot 'onchange' a single option!
>>
>> Well, that is reasonable.
>>
>> The trouble is I have no way of selecting it since the form itself is a
>> dummy: The form action is basically to submit to a new script with
>> hidden variables carefully set to get the desired action depending on
>> the last option selected..
>>
>> onclick() in the <option> itself doesn't work for IE6...
>>
>> Can anyone think of a way to achieve what I want?

>
> Why use a select at all if there is only one option? Wouldn't a
> hidden input make more sense?
>


Firstly because once the user has added more stuff there IS more than
one option, and secondly because there are more options in more boxes
elsewhere.

I used SELECT/OPTION as a handy way of generating various pop up lists
without going to deep into Javascript.

There are several lists, all with one or more options..I need to select
ONE item from ALL the lists and go with that.


The javascript associated with the onchange() or onclick() stuff does
indeed patch in a value to a hidden variable anyway.

Anyway, using onclick() worked except the client is saying it doesn't in
IE7 and I haven't got that to test with. Ah well.

If all else fails I may ye write it with pure javascript pop up klists
and ditch option/select altogether..





 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      09-27-2007
The Natural Philosopher said:
>
>
>This is a nasty one and I can't see my way out of it.
>
>I have a bunch of select statements in a form, and each select statement
>has an onchange="do_something(this)" in it, and this works
>nicely..except when there is only ONE OPTION in a given select.
>It seems you cannot 'onchange' a single option!


So you have the same problem if their choice happens to be the
default selection.
The traditional solution is to provide a dummy first option
with text like "Choose one..." and make your onchange handler
return without action if selectedIndex<1


--

 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      09-27-2007
Lee wrote:
> The Natural Philosopher said:
>>
>> This is a nasty one and I can't see my way out of it.
>>
>> I have a bunch of select statements in a form, and each select statement
>> has an onchange="do_something(this)" in it, and this works
>> nicely..except when there is only ONE OPTION in a given select.
>> It seems you cannot 'onchange' a single option!

>
> So you have the same problem if their choice happens to be the
> default selection.
> The traditional solution is to provide a dummy first option
> with text like "Choose one..." and make your onchange handler
> return without action if selectedIndex<1
>
>

As I said, onclick works..its perfect on my selection of browsers..just
need to see whats happening in Ie7.

The user who reported it is not that computer savvy, so the problem
report is indecipherable till I get on site next week.,
 
Reply With Quote
 
pr
Guest
Posts: n/a
 
      09-27-2007
The Natural Philosopher wrote:
> As I said, onclick works..its perfect on my selection of browsers..just
> need to see whats happening in Ie7.


Not if you use the keyboard it doesn't.
 
Reply With Quote
 
Tim B
Guest
Posts: n/a
 
      09-27-2007

"The Natural Philosopher" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Lee wrote:
> > The Natural Philosopher said:
> >>
> >> This is a nasty one and I can't see my way out of it.
> >>
> >> I have a bunch of select statements in a form, and each select

statement
> >> has an onchange="do_something(this)" in it, and this works
> >> nicely..except when there is only ONE OPTION in a given select.
> >> It seems you cannot 'onchange' a single option!

> >
> > So you have the same problem if their choice happens to be the
> > default selection.
> > The traditional solution is to provide a dummy first option
> > with text like "Choose one..." and make your onchange handler
> > return without action if selectedIndex<1
> >
> >

> As I said, onclick works..its perfect on my selection of browsers..just
> need to see whats happening in Ie7.
>
> The user who reported it is not that computer savvy, so the problem
> report is indecipherable till I get on site next week.,


Why would the user click it if there is only one option?


 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      09-27-2007
Tim B wrote:
> "The Natural Philosopher" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Lee wrote:
>>> The Natural Philosopher said:
>>>> This is a nasty one and I can't see my way out of it.
>>>>
>>>> I have a bunch of select statements in a form, and each select

> statement
>>>> has an onchange="do_something(this)" in it, and this works
>>>> nicely..except when there is only ONE OPTION in a given select.
>>>> It seems you cannot 'onchange' a single option!
>>> So you have the same problem if their choice happens to be the
>>> default selection.
>>> The traditional solution is to provide a dummy first option
>>> with text like "Choose one..." and make your onchange handler
>>> return without action if selectedIndex<1
>>>
>>>

>> As I said, onclick works..its perfect on my selection of browsers..just
>> need to see whats happening in Ie7.
>>
>> The user who reported it is not that computer savvy, so the problem
>> report is indecipherable till I get on site next week.,

>
> Why would the user click it if there is only one option?
>
>

Because as I have patiently explained, there are other options in other
select windows. These are all mutually exclusive.

It turned out to be easier to have multiple windows each with its own
drop down menu, than one enormous scrolling window.

Basically projects - assocaitionns of files and images - are held on a
per user basis.

The master library view lists all the users (who have projects), and
each user has his project set in a window. You select one project in one
window, and then go to it to do whatever.

So is perfectly possible to select a one of many option from a window
with only one element.

As I say, right now <select onclick="select_from(this)">
<option value="x"> I am a lonely option</option>
</select>

works well enough..this.selectedIndex.valueis set correctly if the user
clicks on the box. In firefox, IE6 and Safari.

Well play with it if you like..


<script type="text/javascript" language="JavaScript">
function selectproject()
{
document.getElementsByName("select_project")[0].value="yes";
if(document.getElementsByName("selected_project_id ")[0].value=="")
alert("You didn't select a project to edit!!\n");
else document.forms[0].submit();
}
function newproject()
{
document.getElementsByName("selected_project_id")[0].value="";
document.getElementsByName("select_project")[0].value="yes";
document.forms[0].submit();
}

function select_project_id(here)
{
index=here.selectedIndex;
id=here[index].value;
for(i=0;i< document.forms[0].project_id.length;i++)
{
for (j=0;j<document.forms[0].project_id[i].length;j++)

document.forms[0].project_id[i].options[j].style.color="white";
document.forms[0].project_id[i].style.color="white";
}
here.style.color="yellow";
here[index].style.color="yellow";
document.getElementsByName("selected_project_id")[0].value=id;
}

</script>
<CENTER><TABLE border="0">
<TR><TD width="200px" height="55px" align="center" valign="center"
class="button" >
<b class="s3" onclick=" newproject()" onmouseover="this.className='s4'"
onmouseout="this.className='s3'">NEW PROJECT</b></TD>

<TD height="55px" align="center" valign="center" class="button" >
<b class="s3" onclick=" selectproject()"
onmouseover="this.className='s4'" onmouseout="this.className='s3'">EDIT
SELECTED</b></TD></TR>
<TR><TD align="center"><b>Joe Smith's Projects</b></td>
<TD align="center"><select STYLE="width: 200px" name="project_id"
onclick="select_project_id(this)">
<OPTION value= "11" >More testicles (Sep 4th 07)</OPTION>
<OPTION value= "8" >Database project (Sep 3rd 07)</OPTION>
</select></TD></TR>
<TR><TD align="center"><b>Mike's Projects</b></td>
<TD align="center"><select STYLE="width: 200px" name="project_id"
onclick="select_project_id(this)">
<OPTION value= "5" >another test project (Sep 1st 07)</OPTION>
<OPTION value= "6" >Further tests (Aug 30th 07)</OPTION>

</select></TD></TR>
<TR><TD align="center"><b>Fellow's Projects</b></td>
<TD align="center"><select STYLE="width: 200px" name="project_id"
onclick="select_project_id(this)">
<OPTION value= "12" "Bleriot" (Sep 5th 07)</OPTION>
<OPTION value= "7" >Miles Away (Sep 3rd 07)</OPTION>
<OPTION value= "4" >Test project (Sep 2nd 07)</OPTION>
</SELECT></TD></TR></table>
<input type="hidden" name="selected_project_id" value="">
<input type="hidden" name="select_project" value="no">
</FORM>








 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      09-27-2007
pr wrote:
> The Natural Philosopher wrote:
>> As I said, onclick works..its perfect on my selection of
>> browsers..just need to see whats happening in Ie7.

>
> Not if you use the keyboard it doesn't.


Wasn't able to use the keyboard to do anything anyway in this case.

The help screen tells you to click...
 
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
Force to Only Connect to One Network Only? halo Wireless Networking 2 11-18-2007 04:30 AM
What should one bind data to to display only one record? Darrel ASP .Net 1 11-11-2004 05:08 AM
Multiple asp:buttons on one form but ONLY one should submit? D. Shane Fowlkes ASP .Net 3 02-24-2004 12:17 PM
How can keep only one VM running in One Operating System Sanjay Meher Java 4 02-08-2004 01:35 AM
Re: One to One DVD Duplicator used only 1 month Buckaroo DVD Video 1 08-08-2003 08:05 PM



Advertisments