• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Determining if selected option is valid selection

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a select box that is multiple rows using the size attribute. I don't always fill all of the available rows. I have a function bound to onClick. It was originally onChange, but that didn't work for the initial clicking.

Anyways, I need to determine if the user clicked on an actual valid row, or just on the empty space in the select tag. I've tried checking if the selection is null, but I couldn't get that to work. How can I check this? Thanks.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try <selectBoxId>.options[<selectBoxId>.selectedIndex].value ?
P.S. <selectBoxId> is your select control Id.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
onchange will not fire if the values have not changed. Hence if the user selects the same value,nothing has changed.

You should use onblur.

Eric
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shinelin Samuel wrote:Did you try <selectBoxId>.options[<selectBoxId>.selectedIndex].value ?
P.S. <selectBoxId> is your select control Id.



What would the test be?
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Pascarello wrote:onchange will not fire if the values have not changed. Hence if the user selects the same value,nothing has changed.

You should use onblur.

Eric



onChange wasn't fired at all on the initial change. I would have to select two or three different options before it would initially fire.

I tried onBlur, but it didn't seem to do anything at all.

And I still can't get anything to fire if I use the keyboard.

EDIT: I tried onBlur and it didn't do anything, so I changed it to blur. That worked, but it's not the workflow I need. I need to have the user click on an option and have that fire an event. So far .click is the only thing I've found that will do that. I just need to determine if it's a valid selection or not.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have to loop through as already suggested and look at the selected values.

Eric
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Ended up using .length to test. It returns 1 if it's a valid row and 0 for an empty row.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean length?

Eric
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If it's an empty selection, the statement evaluates as false.
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More accurately, it evaluates to 0, which when used as a boolean expression is interpreted as false.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bai Shen wrote:Got it. Ended up using .length to test. It returns 1 if it's a valid row and 0 for an empty row.


Bai Shen wrote:

If it's an empty selection, the statement evaluates as false.


Bear Bibeault wrote:More accurately, it evaluates to 0, which when used as a boolean expression is interpreted as false.



That's sort of what I said.
 
What? What, what, what? What what tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic