0
votes

i need to get selected option value in JqueryUI SelectMenu out of change event, But i can't get value out of Change function or selectmenu function, how to do it/

  var iSelectedValue;
  var map="";
    $("#drpRegionName").selectmenu({
        change: function (event, ui) {

           iSelectedValue = ui.item.value;

            console.log(iSelectedValue); //Working
        },
        select: function (event, ui) {
            map = $(this).val();
            console.log(map); //working
        }
    });
    console.log(map); // not working
    console.log($(iSelectedValue); // not working

out of the function its undefined.

1
map and iSelectedValue variable gets assigned a value after change/select event. So your outside console return as undefined. - random
First of all iSelectedValue is declared with var' in change` function scope. So this variable is not visible outside of this scope. And as mentioned above by @randomSoul map variable is declared outside of change function. - dganenco
Whatever you want to do with this selected value, should happen inside the change event callback function. - 04FS
@randomSoul , i've tried it also but not working. - Sathishkumar
It will not work. Why would you want to access those variables outside? - random

1 Answers

0
votes

Consider the following example, based on https://jqueryui.com/selectmenu/#product-selection

$(function() {
  var iSelectedValue;
  var map = "";

  function getValues(p) {
    var arr = [];
    $("select", p).each(function(i, el) {
      arr.push({
        id: $(el).attr("id"),
        value: $(el).val()
      });
    });
    return arr;
  }

  $("fieldset select").selectmenu({
    change: function(e, ui) {
      iSelectedValue = getValues($("fieldset"));
      console.log(iSelectedValue);
    }
  });

  iSelectedValue = getValues($("fieldset"));
  console.log(iSelectedValue);
});
fieldset {
  border: 0;
}

label {
  display: block;
  margin: 30px 0 0 0;
}

.overflow {
  height: 200px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<fieldset>
  <label for="speed">Select a speed</label>
  <select name="speed" id="speed">
    <option>Slower</option>
    <option>Slow</option>
    <option selected="selected">Medium</option>
    <option>Fast</option>
    <option>Faster</option>
  </select>

  <label for="files">Select a file</label>
  <select name="files" id="files">
    <optgroup label="Scripts">
      <option value="jquery">jQuery.js</option>
      <option value="jqueryui">ui.jQuery.js</option>
    </optgroup>
    <optgroup label="Other files">
      <option value="somefile">Some unknown file</option>
      <option value="someotherfile">Some other file with a very long option text</option>
    </optgroup>
  </select>

  <label for="number">Select a number</label>
  <select name="number" id="number">
    <option>1</option>
    <option selected="selected">2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option>
    <option>14</option>
    <option>15</option>
    <option>16</option>
    <option>17</option>
    <option>18</option>
    <option>19</option>
  </select>

  <label for="salutation">Select a title</label>
  <select name="salutation" id="salutation">
    <option disabled selected>Please pick one</option>
    <option>Mr.</option>
    <option>Mrs.</option>
    <option>Dr.</option>
    <option>Prof.</option>
    <option>Other</option>
  </select>
</fieldset>

You can see here how we can update the Array when the page loads and then anytime a change event happens for one of the selectMenu items.

Hope that helps.