[jQuery] Re: How to tell if option is selected when it always defaults to first value?

2009-10-26 Thread RobG



On Oct 25, 4:37 pm, Giovanni Battista Lenoci gian...@gmail.com
wrote:
 nick ha scritto: select id=one
     option value=1 selectedone/option
     option value=2two/option
     option value=3three/option
  /select

 The right syntax is :

 option value=1 selected=selectedone/option

Only if the OP is serving XHTML as XHTML, which is rare on the web. If
the markup is being served as HTML (which covers the vast majority of
web pages) then what the OP posted is valid markup.


  If there is no selected then it always defaults to first value. How
  can I tell if the page has loaded and no options have been selected?

The selectedIndex value of the select element will be -1:

URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-85676760 


 This is the way the select element works, there is always a selected
 element, even if you don't interact with the element.

There is no requirement for an option to be selected by default:

URL: http://www.w3.org/TR/html401/interact/forms.html#edef-SELECT 

therefore you should not expect that one will be. It is prudent to
include the selected attribute for the option that should be selected
by default, even if it's the first one, as suggested by the HTML
specification:

| 17.6.1 Pre-selected options
|
| Zero or more choices may be pre-selected for the user.
| [...]
| Since user agent behavior differs, authors should ensure that each
| menu includes a default pre-selected OPTION.


--
Rob


[jQuery] Re: How to tell if option is selected when it always defaults to first value?

2009-10-25 Thread Giovanni Battista Lenoci


nick ha scritto:

select id=one
   option value=1 selectedone/option
   option value=2two/option
   option value=3three/option
/select
  

The right syntax is :

option value=1 selected=selectedone/option


If there is no selected then it always defaults to first value. How
can I tell if the page has loaded and no options have been selected?
  
This is the way the select element works, there is always a selected 
element, even if you don't interact with the element.


You can do this:

select id=one
option value=0Select an option/option
option value=1one/option
option value=2two/option
option value=3three/option
/select

selected_val = parseInt($('#one').val());
if( selected_val == 0) {
  alert('please select an option');
} else {
  alert('You have selected :'+selected_val);
}

Bye

--
gianiaz.net - web solutions
via piedo, 58 - 23020 tresivio (so) - italy
+39 347 7196482