JavaScript Get the id of the selected option selected option jquery,selected option javascript,selected option css,selected

I've 2 dialog forms Add region and Add country. There are <select> options in each modal form. There can be select options which are repeated in each modal form, so I'm putting those in a class and styling it by display:none, so the id's does not get repeated.

When I need to get the id of the item selected, it gives me an undefined value. I'm using $(".region_options").children(":selected").attr("id"); to get the id of the selected option.

Here is my code. and jsFiddle

<!doctype html>
<html lang="en">
<head>

  <link rel="stylesheet" target='_blank' href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" target='_blank' href="/resources/demos/style.css">

  <script>
  $(function() {

    $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,
      buttons: {
        "Create an account": function() {
          var region_id =  $(".region_options").children(":selected").attr("id");
          alert(region_id)
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      },

    });

    $( "#create-user" ).button().click(function() {
        $( "#dialog-form" ).dialog( "open" );
      });
  });


  </script>
</head>
<body>

<div id="dialog-form" title="Create new user">
    <select class="region_options">
        <option>Select Region</option>
        <option id="1">Asia / Pacific</option>
        <option id="2">North America</option>
        <option id="3">sdgvgwqrg</option>
    </select>
</div>

<div id="dialog-form1" title="Create new user" style="display:none">
   <select class="region_options">
        <option>Select Region</option>
        <option id="1">Asia / Pacific</option>
        <option id="2">North America</option>
        <option id="3">sdgvgwqrg</option>
    </select>
</div>


<button id="create-user">Create new user</button>


</body>
</html>
Answer:1

<option> elements are identified by the value attribute, not id.

<select class="region_options">
    <option>Select Region</option>
    <option value="1">Asia / Pacific</option>
    <option value="2">North America</option>
    <option value="3">sdgvgwqrg</option>
</select>

Use val() method to read or write the value of the selected option.

var region = $("#dialog-form .region_options").val();
$("#dialog-form1 .region_options").val(region);
Answer:2

You need to change the line you are using from:

$(".region_options").children(":selected").attr("id");

to:

$(".region_options").children(":selected").parent().parent().attr("id")

Updated Fiddle

Answer:3

It will work with the id I have have worked on your js fiddle the javascript code

`$(function() {

    $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,
      buttons: {
        "Create an account": function() {
          var region_id =  $("#select_region").val();
          alert(region_id);
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      },

    });

    $( "#create-user" ).button().click(function() {
        $( "#dialog-form" ).dialog( "open" );
      });
  });`

and the html code will be :

<div id="dialog-form" title="Create new user">
    <select id="select_region" class="region_options">
        <option>Select Region</option>
        <option value="1">Asia / Pacific</option>
        <option value="2">North America</option>
        <option value="3">sdgvgwqrg</option>
    </select>
</div>

<div id="dialog-form1" title="Create new user" style="display:none">
   <select class="region_options">
        <option>Select Region</option>
        <option value="1">Asia / Pacific</option>
        <option value="2">North America</option>
        <option value="3">sdgvgwqrg</option>
    </select>
</div>


<button id="create-user">Create new user</button>

use id as an attribute in spite of using class <select id="select_region" class="region_options">

Answer:4

As this refers to the modal (your #dialog-form element) we are able to select the select element within it rather than all of them.

$(function() {

    $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,
      buttons: {
        "Create an account": function() {

          var region_id = $(this).find(".region_options").val(); // Solution

          if (!region_id)
            alert('Please select a region.');
          else
            alert('Selected region: ' + region_id);
        },
        "Cancel": function() {
          $( this ).dialog( "close" );
        }
      }
    });

    $( "#create-user" ).button().click(function() {
        $( "#dialog-form" ).dialog( "open" );
      });

  });

See val and find.

Answer:5

I am trying to resize the SVGs in dc.js. In the NASDAQ example in the main page itself. When I change the width/height, it cuts of the svg instead of resizing it. For example, changing width and ...

I am trying to resize the SVGs in dc.js. In the NASDAQ example in the main page itself. When I change the width/height, it cuts of the svg instead of resizing it. For example, changing width and ...

  1. resize js event
  2. resize js jquery
  3. resize js window
  4. resize jscrollpane
  5. resize js library
  6. resize jscrollpane jtable
  7. resize jscrollpane dynamically
  8. resize json
  9. js resize canvas
  10. resize js performance
  11. resize js document

I have a form which uses html and javascript to submit data into a table of an SQL database. So I want to submit what is typed from the user to a local SQLite database that is inside his device. I am ...

I have a form which uses html and javascript to submit data into a table of an SQL database. So I want to submit what is typed from the user to a local SQLite database that is inside his device. I am ...

  1. insert value into table sql
  2. insert value into array
  3. insert value into list python
  4. insert value into string python
  5. insert value into cell vba
  6. insert value into dictionary python
  7. insert value into array python
  8. insert value into array java
  9. insert value into numpy array
  10. insert value into array matlab
  11. insert value into array javascript
  12. insert value into pandas dataframe
  13. insert value into table mysql
  14. insert value into row sql
  15. insert value into temp table
  16. insert values into vector c++
  17. insert value into map c++
  18. insert value into array php
  19. insert value into vector r
  20. insert value into array c++

http://jsfiddle.net/PhilFromHeck/KzSxT/ In this fiddle, you can see at line 38 in the Javascript that I've attempted to make a comparison that isn't working. I believe it because one of the variables ...

http://jsfiddle.net/PhilFromHeck/KzSxT/ In this fiddle, you can see at line 38 in the Javascript that I've attempted to make a comparison that isn't working. I believe it because one of the variables ...

  1. javascript compare objects in array
  2. javascript compare objects for equality
  3. javascript compare objects by value
  4. javascript compare objects difference
  5. javascript compare objects by reference
  6. javascript compare objects by keys
  7. javascript compare objects es6
  8. javascript compare objects lodash
  9. javascript compare objects properties
  10. javascript compare objects
  11. javascript compare objects ignore property
  12. javascript compare objects stringify
  13. javascript compare objects shallow
  14. javascript compare objects hash
  15. javascript compare objects json
  16. javascript compare objects fast
  17. javascript compare objects online
  18. javascript compare objects greater than
  19. javascript compare objects npm
  20. javascript compare objects for identity

I need to use an Image instead of using dropbox default chooser button. I have gone through their API docs and I couldn't find any such ways to use other elements for dropbox chooser. Can anyone ...

I need to use an Image instead of using dropbox default chooser button. I have gone through their API docs and I couldn't find any such ways to use other elements for dropbox chooser. Can anyone ...