Home
Categories
HTML Select
CSS Class
General
CheckBox
HTML Textarea
DropDown
jQuery UI
DatePicker
Facebook
HTML Form
HTML DIV
ASP.Net
JSON
Radio
Regular Expressions
HTML Multiselect ListBox
HTML TextBox
RadioButtonList
CheckBoxList
HTML Anchor
Master Pages
HTML5
Bootstrap
Image
FileUpload
HTML Span
ListBox
jQuery
Table
Button
Password TextBox
Sponsored
XML
Forums
Contact
Search
Search
Home
Categories
Forums
Contact
Search
Check if dropdown option other than default option is selected using jQuery
23 Jan 2012
Mudassar Khan
0
Comments
17738 Views
HTML Select
DropDown
Follow @jQueryFaqs
Follow @jQueryFaqs
Here Mudassar Ahmed Khan has explained how to check whether the dropdown option is selected or not, if it is selected then it should be other than the default option using jQuery and selectedindex property.
Follow @jQueryFaqs
Follow @jQueryFaqs
The below code snippet explains how to check whether the HTML SELECT Dropdown item other than the default item is selected or not using jQuery
<
html
xmlns
="http://www.w3.org/1999/xhtml">
<
head
>
<
script
type
="text/javascript"
src
="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></
script
>
<
title
></
title
>
<
script
type
=
"text/javascript">
$(
"#demo"
).live(
"click"
,
function
() {
if
($(
"#ddl"
)[0].selectedIndex <= 0) {
alert(
"Please select"
);
}
});
</
script
>
</
head
>
<
body
>
<
form
id
="form1">
<
select
id
="ddl">
<
option
value
="0">
Please select
</
option
>
<
option
value
="1">
1
</
option
>
<
option
value
="2">
2
</
option
>
<
option
value
="3">
3
</
option
>
</
select
>
<
input
type
=
"button"
id
=
"demo"
value
=
"Demo"
/>
</
form
>
</
body
>
</
html
>
Explanation:
In the above code snippet click event handler has been attached to the HTML input button with ID
demo
. When the button is clicked we check whether the
selectedIndex
of the HTML SELECT Dropdown is 0 or -1. The selectedIndex is -1 when no action has been taken on the HTML SELECT Dropdown. Once an option is selected the selectedIndex is set to the ZERO BASED INDEX of that option. Since the 0
th
Option is generally the default option I have checked for both 0 and -1 values.
Demo:
Please select
1
2
3
Related Articles
Get Selected Value of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get selected value of HTML SELECT using jQuery
Get Selected Option Value of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get selected option value of HTML SELECT using jQuery
Get Selected Text of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get selected text or inner html of HTML SELECT using jQuery
Get Selected Option Text of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get selected option text of HTML SELECT using jQuery
Get Selected Item of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get selected item of HTML SELECT using jQuery
Get Selected Option of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get selected option of HTML SELECT using jQuery
Get SelectedIndex of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get selectedIndex of HTML SELECT using jQuery
Get Option SelectedIndex of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get option selected index of HTML SELECT using jQuery
Get Index of Selected Option of HTML SELECT using jQuery
Here Mudassar Ahmed Khan has explained how to get index of selected option of HTML SELECT using jQuery
Check whether an Option is Selected in HTML Select using jQuery
Here Mudassar Ahmed Khan has explained how to check whether an Option is Selected in HTML Select using jQuery
jQuery - Set Option as selected using its Value in HTML SELECT
Here Mudassar Ahmed Khan has explained how to set Option as selected using its Value in HTML SELECT using jQuery.
Get values of all selected checkboxes by name using jQuery
Here Mudassar Ahmed Khan has provided snippet to get values of all selected checkboxes by name using jQuery
Get ids of all selected checkboxes using name in jQuery
Here Mudassar Ahmed Khan has provided snippet to get ids of all selected checkboxes using name in jQuery
Get values of all selected checkboxes by class name using jQuery
Here Mudassar Ahmed Khan has provided snippet to get values of all selected checkboxes by class name using jQuery
Get ids of all selected checkboxes using class name in jQuery
Here Mudassar Ahmed Khan has provided snippet to get ids of all selected checkboxes using class name in jQuery
Comments
No comments have been added to this article.
Add Comments
Thank you for the feedback. The comment is now awaiting moderation. You will be notified via email when the author replies to your comment.
Please select a comment to reply
You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment
Please note that all comments are moderated and will be deleted if they are
Not relavant to the article
Spam
Advertising campaigns or links to other sites
Abusive content.
Please do not post code, scripts or snippets.
Follow @jQueryFaqs
Follow @jQueryFaqs
Name
Required
Email
Required
Invalid Email Address
Comment
Required
Security code:
Required
Invalid security code.
Add Comment
Message from Author
Message from Author
Subscribe
Follow @jQueryFaqs
Follow @jQueryFaqs
Error Details
Disclaimer
This site makes use of Cookies. Please refer
Privacy Policy
for more details.