• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JQuery Selected Selector Example

May 27, 2014 //  by Krishna Srinivasan//  Leave a Comment

This tutorial explains usage of selected selector in jQuery. The selected selector event occurs when an element is selected in the document. It matches the elements that are of type selected in a page. It works for option elements that are pre-selected. The pre-selected option will be displayed first in the drop down list. The selected selector works for only option elements; it does not work for checkbox and radio box; use checked selector instead for them instead of using selected selector.

JQuery Selected Selector Syntax

$(“: selected”)

JQuery Selected Selector Example

<!doctype html>
<head>
<title>JQuery Selected Selector</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Selected Selector Example</h2>
<style>
   div{
      color:green;
   }
</style>
<script type="text/javascript">
   $(document).ready(function(){
      $(":selected").css("background-color","red");
        $("select").change(function(){
          $("div").text($("select option:selected").val()+ " is selected");
        });
   });
</script>
<body>
  <select>
      <option value="bangalore">Bangalore</option>
      <option value="belgaum" selected="selected">Belgaum</option>
      <option value="hubli">Hubli</option>
      <option value="mangalore">Mangalore</option>
      <option value="gulbarga">Gulbarga</option>
</select>
<div></div>
</body>
</html>
  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“:selected”).css(“background-color”,”red”); line defines selected selector which select the element that is pre-selected. The pre-selected element will display with red color background in the drop down list.
  • $(“div”).text($(“select option:selected”).val()+ ” is selected”); line defines the value of selected element by using val() method. The val() method returns value of attribute of the matched element in the document.
  • When we run the above script, we will get drop down list with list of options. The pre-selected element will display with red color background and when we select different element every time, the selected element will get display with green color.

JQuery Selected Selector Demo

When you run the above example, you would get the following output:

  • Run JQuery Selected Selector Demo

JQuery Selected Selector Example

Category: jQueryTag: JQuery Selectors

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: «jquery JQuery Multiple Attribute Selector Example
Next Post: Spring Data REST + GemFire + jQuery Integration »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact