To get the first 2 html values of a select list Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Iterate through <select> optionsWhat are valid values for the id attribute in HTML?Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?How do you get a timestamp in JavaScript?How to get the children of the $(this) selector?How can I know which radio button is selected via jQuery?How can I get query string values in JavaScript?How do I make the first letter of a string uppercase in JavaScript?Get selected value in dropdown list using JavaScript?Get selected text from a drop-down list (select box) using jQueryWhy does HTML think “chucknorris” is a color?

ArcGIS Pro Python arcpy.CreatePersonalGDB_management

Why do we bend a book to keep it straight?

A term for a woman complaining about things/begging in a cute/childish way

Generate an RGB colour grid

Why does the remaining Rebel fleet at the end of Rogue One seem dramatically larger than the one in A New Hope?

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

Selecting user stories during sprint planning

How were pictures turned from film to a big picture in a picture frame before digital scanning?

How to react to hostile behavior from a senior developer?

Dating a Former Employee

Do any jurisdictions seriously consider reclassifying social media websites as publishers?

What is this clumpy 20-30cm high yellow-flowered plant?

What was the first language to use conditional keywords?

Is CEO the "profession" with the most psychopaths?

AppleTVs create a chatty alternate WiFi network

How would a mousetrap for use in space work?

Effects on objects due to a brief relocation of massive amounts of mass

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

How much damage would a cupful of neutron star matter do to the Earth?

How can I reduce the gap between left and right of cdot with a macro?

Why should I vote and accept answers?

Drawing without replacement: why is the order of draw irrelevant?

Take 2! Is this homebrew Lady of Pain warlock patron balanced?

What does it mean that physics no longer uses mechanical models to describe phenomena?



To get the first 2 html values of a select list



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Iterate through <select> optionsWhat are valid values for the id attribute in HTML?Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?How do you get a timestamp in JavaScript?How to get the children of the $(this) selector?How can I know which radio button is selected via jQuery?How can I get query string values in JavaScript?How do I make the first letter of a string uppercase in JavaScript?Get selected value in dropdown list using JavaScript?Get selected text from a drop-down list (select box) using jQueryWhy does HTML think “chucknorris” is a color?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Is there a way I can select only the top 2 select value options instead of all 4 like how it is happening in the below example? I need



<option value="volvo">Volvo</option>
<option value="saab">Saab</option>


on button click.






 $("#btnTest").click(function()
var options = $("#test").html();
console.log(options);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="test">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<button id="btnTest" type="button">Click Me!</button>












share|improve this question






















  • maybe this will help you link.

    – ilia
    Mar 8 at 20:54

















0















Is there a way I can select only the top 2 select value options instead of all 4 like how it is happening in the below example? I need



<option value="volvo">Volvo</option>
<option value="saab">Saab</option>


on button click.






 $("#btnTest").click(function()
var options = $("#test").html();
console.log(options);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="test">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<button id="btnTest" type="button">Click Me!</button>












share|improve this question






















  • maybe this will help you link.

    – ilia
    Mar 8 at 20:54













0












0








0








Is there a way I can select only the top 2 select value options instead of all 4 like how it is happening in the below example? I need



<option value="volvo">Volvo</option>
<option value="saab">Saab</option>


on button click.






 $("#btnTest").click(function()
var options = $("#test").html();
console.log(options);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="test">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<button id="btnTest" type="button">Click Me!</button>












share|improve this question














Is there a way I can select only the top 2 select value options instead of all 4 like how it is happening in the below example? I need



<option value="volvo">Volvo</option>
<option value="saab">Saab</option>


on button click.






 $("#btnTest").click(function()
var options = $("#test").html();
console.log(options);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="test">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<button id="btnTest" type="button">Click Me!</button>








 $("#btnTest").click(function()
var options = $("#test").html();
console.log(options);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="test">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<button id="btnTest" type="button">Click Me!</button>





 $("#btnTest").click(function()
var options = $("#test").html();
console.log(options);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="test">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<button id="btnTest" type="button">Click Me!</button>






javascript jquery html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 20:45









TheFallenOneTheFallenOne

7241829




7241829












  • maybe this will help you link.

    – ilia
    Mar 8 at 20:54

















  • maybe this will help you link.

    – ilia
    Mar 8 at 20:54
















maybe this will help you link.

– ilia
Mar 8 at 20:54





maybe this will help you link.

– ilia
Mar 8 at 20:54












2 Answers
2






active

oldest

votes


















1














You can use the jQuery :lt() selector to grab only the first two elements, and chain a .toArray() to turn it into an array of HTML.



var $options = $("#test option:lt(2)").toArray();





 $("#btnTest").click(function()
var options = $("#test option:lt(2)").toArray();
console.log(options);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="test">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<button id="btnTest" type="button">Click Me!</button>





Or, as a string...



var options = $("#test option:lt(2)")
.toArray()
.map(function(e)return e.outerHTML)
.join("n");





$("#btnTest").click(function()
var options = $("#test option:lt(2)")
.toArray()
.map(function(e)return e.outerHTML) //get outerHTML of each element
.join("n"); //join them with a line-break
console.log(options);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="test">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<button id="btnTest" type="button">Click Me!</button>








share|improve this answer
































    1














    You can use options property for this



     $("#btnTest").click(function()
    var options = Array.from($("#test")[0].options).slice(0, 2);
    console.log(options)
    );





    $("#btnTest").click(function() 
    var options = Array.from($("#test")[0].options).slice(0, 2);
    console.log(options)
    );

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <select id="test">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
    </select>

    <button id="btnTest" type="button">Click Me!</button>








    share|improve this answer

























      Your Answer






      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: "1"
      ;
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070743%2fto-get-the-first-2-html-values-of-a-select-list%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can use the jQuery :lt() selector to grab only the first two elements, and chain a .toArray() to turn it into an array of HTML.



      var $options = $("#test option:lt(2)").toArray();





       $("#btnTest").click(function()
      var options = $("#test option:lt(2)").toArray();
      console.log(options);
      );

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

      <select id="test">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
      </select>

      <button id="btnTest" type="button">Click Me!</button>





      Or, as a string...



      var options = $("#test option:lt(2)")
      .toArray()
      .map(function(e)return e.outerHTML)
      .join("n");





      $("#btnTest").click(function()
      var options = $("#test option:lt(2)")
      .toArray()
      .map(function(e)return e.outerHTML) //get outerHTML of each element
      .join("n"); //join them with a line-break
      console.log(options);
      );

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

      <select id="test">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
      </select>

      <button id="btnTest" type="button">Click Me!</button>








      share|improve this answer





























        1














        You can use the jQuery :lt() selector to grab only the first two elements, and chain a .toArray() to turn it into an array of HTML.



        var $options = $("#test option:lt(2)").toArray();





         $("#btnTest").click(function()
        var options = $("#test option:lt(2)").toArray();
        console.log(options);
        );

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

        <select id="test">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
        <option value="audi">Audi</option>
        </select>

        <button id="btnTest" type="button">Click Me!</button>





        Or, as a string...



        var options = $("#test option:lt(2)")
        .toArray()
        .map(function(e)return e.outerHTML)
        .join("n");





        $("#btnTest").click(function()
        var options = $("#test option:lt(2)")
        .toArray()
        .map(function(e)return e.outerHTML) //get outerHTML of each element
        .join("n"); //join them with a line-break
        console.log(options);
        );

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

        <select id="test">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
        <option value="audi">Audi</option>
        </select>

        <button id="btnTest" type="button">Click Me!</button>








        share|improve this answer



























          1












          1








          1







          You can use the jQuery :lt() selector to grab only the first two elements, and chain a .toArray() to turn it into an array of HTML.



          var $options = $("#test option:lt(2)").toArray();





           $("#btnTest").click(function()
          var options = $("#test option:lt(2)").toArray();
          console.log(options);
          );

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <select id="test">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>

          <button id="btnTest" type="button">Click Me!</button>





          Or, as a string...



          var options = $("#test option:lt(2)")
          .toArray()
          .map(function(e)return e.outerHTML)
          .join("n");





          $("#btnTest").click(function()
          var options = $("#test option:lt(2)")
          .toArray()
          .map(function(e)return e.outerHTML) //get outerHTML of each element
          .join("n"); //join them with a line-break
          console.log(options);
          );

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <select id="test">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>

          <button id="btnTest" type="button">Click Me!</button>








          share|improve this answer















          You can use the jQuery :lt() selector to grab only the first two elements, and chain a .toArray() to turn it into an array of HTML.



          var $options = $("#test option:lt(2)").toArray();





           $("#btnTest").click(function()
          var options = $("#test option:lt(2)").toArray();
          console.log(options);
          );

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <select id="test">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>

          <button id="btnTest" type="button">Click Me!</button>





          Or, as a string...



          var options = $("#test option:lt(2)")
          .toArray()
          .map(function(e)return e.outerHTML)
          .join("n");





          $("#btnTest").click(function()
          var options = $("#test option:lt(2)")
          .toArray()
          .map(function(e)return e.outerHTML) //get outerHTML of each element
          .join("n"); //join them with a line-break
          console.log(options);
          );

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <select id="test">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>

          <button id="btnTest" type="button">Click Me!</button>








           $("#btnTest").click(function()
          var options = $("#test option:lt(2)").toArray();
          console.log(options);
          );

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <select id="test">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>

          <button id="btnTest" type="button">Click Me!</button>





           $("#btnTest").click(function()
          var options = $("#test option:lt(2)").toArray();
          console.log(options);
          );

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <select id="test">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>

          <button id="btnTest" type="button">Click Me!</button>





          $("#btnTest").click(function()
          var options = $("#test option:lt(2)")
          .toArray()
          .map(function(e)return e.outerHTML) //get outerHTML of each element
          .join("n"); //join them with a line-break
          console.log(options);
          );

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <select id="test">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>

          <button id="btnTest" type="button">Click Me!</button>





          $("#btnTest").click(function()
          var options = $("#test option:lt(2)")
          .toArray()
          .map(function(e)return e.outerHTML) //get outerHTML of each element
          .join("n"); //join them with a line-break
          console.log(options);
          );

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <select id="test">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>

          <button id="btnTest" type="button">Click Me!</button>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 21:07

























          answered Mar 8 at 21:00









          Tyler RoperTyler Roper

          14.5k32242




          14.5k32242























              1














              You can use options property for this



               $("#btnTest").click(function()
              var options = Array.from($("#test")[0].options).slice(0, 2);
              console.log(options)
              );





              $("#btnTest").click(function() 
              var options = Array.from($("#test")[0].options).slice(0, 2);
              console.log(options)
              );

              <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

              <select id="test">
              <option value="volvo">Volvo</option>
              <option value="saab">Saab</option>
              <option value="mercedes">Mercedes</option>
              <option value="audi">Audi</option>
              </select>

              <button id="btnTest" type="button">Click Me!</button>








              share|improve this answer





























                1














                You can use options property for this



                 $("#btnTest").click(function()
                var options = Array.from($("#test")[0].options).slice(0, 2);
                console.log(options)
                );





                $("#btnTest").click(function() 
                var options = Array.from($("#test")[0].options).slice(0, 2);
                console.log(options)
                );

                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                <select id="test">
                <option value="volvo">Volvo</option>
                <option value="saab">Saab</option>
                <option value="mercedes">Mercedes</option>
                <option value="audi">Audi</option>
                </select>

                <button id="btnTest" type="button">Click Me!</button>








                share|improve this answer



























                  1












                  1








                  1







                  You can use options property for this



                   $("#btnTest").click(function()
                  var options = Array.from($("#test")[0].options).slice(0, 2);
                  console.log(options)
                  );





                  $("#btnTest").click(function() 
                  var options = Array.from($("#test")[0].options).slice(0, 2);
                  console.log(options)
                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                  <select id="test">
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
                  </select>

                  <button id="btnTest" type="button">Click Me!</button>








                  share|improve this answer















                  You can use options property for this



                   $("#btnTest").click(function()
                  var options = Array.from($("#test")[0].options).slice(0, 2);
                  console.log(options)
                  );





                  $("#btnTest").click(function() 
                  var options = Array.from($("#test")[0].options).slice(0, 2);
                  console.log(options)
                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                  <select id="test">
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
                  </select>

                  <button id="btnTest" type="button">Click Me!</button>








                  $("#btnTest").click(function() 
                  var options = Array.from($("#test")[0].options).slice(0, 2);
                  console.log(options)
                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                  <select id="test">
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
                  </select>

                  <button id="btnTest" type="button">Click Me!</button>





                  $("#btnTest").click(function() 
                  var options = Array.from($("#test")[0].options).slice(0, 2);
                  console.log(options)
                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                  <select id="test">
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
                  </select>

                  <button id="btnTest" type="button">Click Me!</button>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 8 at 20:55









                  j08691

                  168k20199215




                  168k20199215










                  answered Mar 8 at 20:53









                  maksimrmaksimr

                  675514




                  675514



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070743%2fto-get-the-first-2-html-values-of-a-select-list%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      1928 у кіно

                      Захаров Федір Захарович

                      Ель Греко