Wednesday 19 February 2014

Selected option value jquery

To work with the jquery you need to take the refrence of Jquery into your HTML code like below.
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
You need to create the list box and put the option into List box.


<select name="garden" multiple="multiple" style="width: 100px; height: 200px">
        <option>Bob</option>
        <option selected="selected">Eric</option>
        <option>Danny</option>
        <option selected="selected">Arran</option>
        <option>Soni</option>
        <option>Ali</option>
        <option>Ani</option>
        <option>Rohan</option>
         <option>Ram</option>
    </select>
Full code to get the selection of option by Jquery;



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        div
        {
            color: Green;
        }
    </style>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
    <h1>
        List Box selection</h1>
    <select name="garden" multiple="multiple" style="width: 100px; height: 200px">
        <option>Bob</option>
        <option selected="selected">Eric</option>
        <option>Danny</option>
        <option selected="selected">Arran</option>
        <option>Soni</option>
        <option>Ali</option>
        <option>Ani</option>
        <option>Rohan</option>
         <option>Ram</option>
    </select> <br />
   <b>Selected Text :</b>
    <div>
    </div>
    <script>
        $("select")
  .change(function () {
      var str = "";
      $("select option:selected").each(function () {
          str += $(this).text() + ", ";
      });
      $("div").text(str);
  })
  .trigger("change");
    </script>
</body>

</html>

Output will display like Below;

No comments:

Post a Comment