Unify code that works with selectmenu

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I have span and select in my view



Here is code of it






$(function() 
var selectSpeed = $('#speed'),
selectTest = $('#test');

selectSpeed.selectmenu();
selectTest.selectmenu();

$("#speed-button").on("mousedown", function()
selectSpeed.selectmenu("open");
);
$('#speed-button').on("click", function()
selectSpeed.selectmenu("open");
);

$(document).on("mouseup", "#speed-menu .ui-menu-item-wrapper", function()
selectSpeed.val($(this).text().substr(0, 1)).change();
selectSpeed.selectmenu("close");
selectSpeed.selectmenu("refresh");
);


$("#test-button").on("mousedown", function()
selectTest.selectmenu("open");
);
$('#test-button').on("click", function()
selectTest.selectmenu("open");
);

$(document).on("mouseup", "#test-menu .ui-menu-item-wrapper", function()
selectTest.val($(this).text().substr(0, 1)).change();
selectTest.selectmenu("close");
selectTest.selectmenu("refresh");
);
);

fieldset 
border: 0;


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


.overflow
height: 200px;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<div class="demo">
<form action="#">
<fieldset>
<select name="speed" id="speed">
<option selected="selected" value="1">1 vuxen</option>
<option value="2">2 vuxna</option>
<option value="3">3 vuxna</option>
<option value="4">4 vuxna</option>
<option value="5">5 vuxna</option>
<option value="6">6 vuxna</option>
<option value="7">7 vuxna</option>
<option value="8">8 vuxna</option>
<option value="9">9 vuxna</option>
</select>
<select name="test" id="test">
<option selected="selected" value="1">1 test</option>
<option value="2">2 test</option>
<option value="3">3 test</option>
<option value="4">4 test</option>
<option value="5">5 test</option>
<option value="6">6 test</option>
<option value="7">7 test</option>
<option value="8">8 test</option>
<option value="9">9 test</option>
</select>
</fieldset>
</form>
</div>





For example I click span holding the mouse and then I move it to 2 vuxna and release it. And I would like to get 2 vuxna.



And same functionality for second select



But If I will have 3 selects, I need to define 3 variable and 3 $(document).on(.



That's not great. I wonder if I have any ways to unify this code?







share|improve this question

























    up vote
    0
    down vote

    favorite












    I have span and select in my view



    Here is code of it






    $(function() 
    var selectSpeed = $('#speed'),
    selectTest = $('#test');

    selectSpeed.selectmenu();
    selectTest.selectmenu();

    $("#speed-button").on("mousedown", function()
    selectSpeed.selectmenu("open");
    );
    $('#speed-button').on("click", function()
    selectSpeed.selectmenu("open");
    );

    $(document).on("mouseup", "#speed-menu .ui-menu-item-wrapper", function()
    selectSpeed.val($(this).text().substr(0, 1)).change();
    selectSpeed.selectmenu("close");
    selectSpeed.selectmenu("refresh");
    );


    $("#test-button").on("mousedown", function()
    selectTest.selectmenu("open");
    );
    $('#test-button').on("click", function()
    selectTest.selectmenu("open");
    );

    $(document).on("mouseup", "#test-menu .ui-menu-item-wrapper", function()
    selectTest.val($(this).text().substr(0, 1)).change();
    selectTest.selectmenu("close");
    selectTest.selectmenu("refresh");
    );
    );

    fieldset 
    border: 0;


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


    .overflow
    height: 200px;

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

    <div class="demo">
    <form action="#">
    <fieldset>
    <select name="speed" id="speed">
    <option selected="selected" value="1">1 vuxen</option>
    <option value="2">2 vuxna</option>
    <option value="3">3 vuxna</option>
    <option value="4">4 vuxna</option>
    <option value="5">5 vuxna</option>
    <option value="6">6 vuxna</option>
    <option value="7">7 vuxna</option>
    <option value="8">8 vuxna</option>
    <option value="9">9 vuxna</option>
    </select>
    <select name="test" id="test">
    <option selected="selected" value="1">1 test</option>
    <option value="2">2 test</option>
    <option value="3">3 test</option>
    <option value="4">4 test</option>
    <option value="5">5 test</option>
    <option value="6">6 test</option>
    <option value="7">7 test</option>
    <option value="8">8 test</option>
    <option value="9">9 test</option>
    </select>
    </fieldset>
    </form>
    </div>





    For example I click span holding the mouse and then I move it to 2 vuxna and release it. And I would like to get 2 vuxna.



    And same functionality for second select



    But If I will have 3 selects, I need to define 3 variable and 3 $(document).on(.



    That's not great. I wonder if I have any ways to unify this code?







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have span and select in my view



      Here is code of it






      $(function() 
      var selectSpeed = $('#speed'),
      selectTest = $('#test');

      selectSpeed.selectmenu();
      selectTest.selectmenu();

      $("#speed-button").on("mousedown", function()
      selectSpeed.selectmenu("open");
      );
      $('#speed-button').on("click", function()
      selectSpeed.selectmenu("open");
      );

      $(document).on("mouseup", "#speed-menu .ui-menu-item-wrapper", function()
      selectSpeed.val($(this).text().substr(0, 1)).change();
      selectSpeed.selectmenu("close");
      selectSpeed.selectmenu("refresh");
      );


      $("#test-button").on("mousedown", function()
      selectTest.selectmenu("open");
      );
      $('#test-button').on("click", function()
      selectTest.selectmenu("open");
      );

      $(document).on("mouseup", "#test-menu .ui-menu-item-wrapper", function()
      selectTest.val($(this).text().substr(0, 1)).change();
      selectTest.selectmenu("close");
      selectTest.selectmenu("refresh");
      );
      );

      fieldset 
      border: 0;


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


      .overflow
      height: 200px;

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

      <div class="demo">
      <form action="#">
      <fieldset>
      <select name="speed" id="speed">
      <option selected="selected" value="1">1 vuxen</option>
      <option value="2">2 vuxna</option>
      <option value="3">3 vuxna</option>
      <option value="4">4 vuxna</option>
      <option value="5">5 vuxna</option>
      <option value="6">6 vuxna</option>
      <option value="7">7 vuxna</option>
      <option value="8">8 vuxna</option>
      <option value="9">9 vuxna</option>
      </select>
      <select name="test" id="test">
      <option selected="selected" value="1">1 test</option>
      <option value="2">2 test</option>
      <option value="3">3 test</option>
      <option value="4">4 test</option>
      <option value="5">5 test</option>
      <option value="6">6 test</option>
      <option value="7">7 test</option>
      <option value="8">8 test</option>
      <option value="9">9 test</option>
      </select>
      </fieldset>
      </form>
      </div>





      For example I click span holding the mouse and then I move it to 2 vuxna and release it. And I would like to get 2 vuxna.



      And same functionality for second select



      But If I will have 3 selects, I need to define 3 variable and 3 $(document).on(.



      That's not great. I wonder if I have any ways to unify this code?







      share|improve this question











      I have span and select in my view



      Here is code of it






      $(function() 
      var selectSpeed = $('#speed'),
      selectTest = $('#test');

      selectSpeed.selectmenu();
      selectTest.selectmenu();

      $("#speed-button").on("mousedown", function()
      selectSpeed.selectmenu("open");
      );
      $('#speed-button').on("click", function()
      selectSpeed.selectmenu("open");
      );

      $(document).on("mouseup", "#speed-menu .ui-menu-item-wrapper", function()
      selectSpeed.val($(this).text().substr(0, 1)).change();
      selectSpeed.selectmenu("close");
      selectSpeed.selectmenu("refresh");
      );


      $("#test-button").on("mousedown", function()
      selectTest.selectmenu("open");
      );
      $('#test-button').on("click", function()
      selectTest.selectmenu("open");
      );

      $(document).on("mouseup", "#test-menu .ui-menu-item-wrapper", function()
      selectTest.val($(this).text().substr(0, 1)).change();
      selectTest.selectmenu("close");
      selectTest.selectmenu("refresh");
      );
      );

      fieldset 
      border: 0;


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


      .overflow
      height: 200px;

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

      <div class="demo">
      <form action="#">
      <fieldset>
      <select name="speed" id="speed">
      <option selected="selected" value="1">1 vuxen</option>
      <option value="2">2 vuxna</option>
      <option value="3">3 vuxna</option>
      <option value="4">4 vuxna</option>
      <option value="5">5 vuxna</option>
      <option value="6">6 vuxna</option>
      <option value="7">7 vuxna</option>
      <option value="8">8 vuxna</option>
      <option value="9">9 vuxna</option>
      </select>
      <select name="test" id="test">
      <option selected="selected" value="1">1 test</option>
      <option value="2">2 test</option>
      <option value="3">3 test</option>
      <option value="4">4 test</option>
      <option value="5">5 test</option>
      <option value="6">6 test</option>
      <option value="7">7 test</option>
      <option value="8">8 test</option>
      <option value="9">9 test</option>
      </select>
      </fieldset>
      </form>
      </div>





      For example I click span holding the mouse and then I move it to 2 vuxna and release it. And I would like to get 2 vuxna.



      And same functionality for second select



      But If I will have 3 selects, I need to define 3 variable and 3 $(document).on(.



      That's not great. I wonder if I have any ways to unify this code?






      $(function() 
      var selectSpeed = $('#speed'),
      selectTest = $('#test');

      selectSpeed.selectmenu();
      selectTest.selectmenu();

      $("#speed-button").on("mousedown", function()
      selectSpeed.selectmenu("open");
      );
      $('#speed-button').on("click", function()
      selectSpeed.selectmenu("open");
      );

      $(document).on("mouseup", "#speed-menu .ui-menu-item-wrapper", function()
      selectSpeed.val($(this).text().substr(0, 1)).change();
      selectSpeed.selectmenu("close");
      selectSpeed.selectmenu("refresh");
      );


      $("#test-button").on("mousedown", function()
      selectTest.selectmenu("open");
      );
      $('#test-button').on("click", function()
      selectTest.selectmenu("open");
      );

      $(document).on("mouseup", "#test-menu .ui-menu-item-wrapper", function()
      selectTest.val($(this).text().substr(0, 1)).change();
      selectTest.selectmenu("close");
      selectTest.selectmenu("refresh");
      );
      );

      fieldset 
      border: 0;


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


      .overflow
      height: 200px;

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

      <div class="demo">
      <form action="#">
      <fieldset>
      <select name="speed" id="speed">
      <option selected="selected" value="1">1 vuxen</option>
      <option value="2">2 vuxna</option>
      <option value="3">3 vuxna</option>
      <option value="4">4 vuxna</option>
      <option value="5">5 vuxna</option>
      <option value="6">6 vuxna</option>
      <option value="7">7 vuxna</option>
      <option value="8">8 vuxna</option>
      <option value="9">9 vuxna</option>
      </select>
      <select name="test" id="test">
      <option selected="selected" value="1">1 test</option>
      <option value="2">2 test</option>
      <option value="3">3 test</option>
      <option value="4">4 test</option>
      <option value="5">5 test</option>
      <option value="6">6 test</option>
      <option value="7">7 test</option>
      <option value="8">8 test</option>
      <option value="9">9 test</option>
      </select>
      </fieldset>
      </form>
      </div>





      $(function() 
      var selectSpeed = $('#speed'),
      selectTest = $('#test');

      selectSpeed.selectmenu();
      selectTest.selectmenu();

      $("#speed-button").on("mousedown", function()
      selectSpeed.selectmenu("open");
      );
      $('#speed-button').on("click", function()
      selectSpeed.selectmenu("open");
      );

      $(document).on("mouseup", "#speed-menu .ui-menu-item-wrapper", function()
      selectSpeed.val($(this).text().substr(0, 1)).change();
      selectSpeed.selectmenu("close");
      selectSpeed.selectmenu("refresh");
      );


      $("#test-button").on("mousedown", function()
      selectTest.selectmenu("open");
      );
      $('#test-button').on("click", function()
      selectTest.selectmenu("open");
      );

      $(document).on("mouseup", "#test-menu .ui-menu-item-wrapper", function()
      selectTest.val($(this).text().substr(0, 1)).change();
      selectTest.selectmenu("close");
      selectTest.selectmenu("refresh");
      );
      );

      fieldset 
      border: 0;


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


      .overflow
      height: 200px;

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

      <div class="demo">
      <form action="#">
      <fieldset>
      <select name="speed" id="speed">
      <option selected="selected" value="1">1 vuxen</option>
      <option value="2">2 vuxna</option>
      <option value="3">3 vuxna</option>
      <option value="4">4 vuxna</option>
      <option value="5">5 vuxna</option>
      <option value="6">6 vuxna</option>
      <option value="7">7 vuxna</option>
      <option value="8">8 vuxna</option>
      <option value="9">9 vuxna</option>
      </select>
      <select name="test" id="test">
      <option selected="selected" value="1">1 test</option>
      <option value="2">2 test</option>
      <option value="3">3 test</option>
      <option value="4">4 test</option>
      <option value="5">5 test</option>
      <option value="6">6 test</option>
      <option value="7">7 test</option>
      <option value="8">8 test</option>
      <option value="9">9 test</option>
      </select>
      </fieldset>
      </form>
      </div>








      share|improve this question










      share|improve this question




      share|improve this question









      asked May 10 at 13:27









      Balance

      11




      11

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194105%2funify-code-that-works-with-selectmenu%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194105%2funify-code-that-works-with-selectmenu%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?