How to use a variable inside $('#id option:eq(VARIABLE)')?












-1















I basically want to set some dropdown menu to a certain index by comparing to the respective value.
Here's the code:






function InitializeLieblingsplatzOptions(){
$.post('../include/retrieveLieblingsplatz.php',{
//nothing to transmit
}).then((data) => {
data = JSON.parse(data)
console.log("data in retrieveLieblingsplatz.php is ", data)

//let test = $('#liebraum').find("test")
$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)



})
}





the data returned from the ajax contains two strings which represent a roomname and seatnumber. There are 2 dropdown menus on this site which contain all names of all rooms and all available seats of the respective room.
I now want to use the 2 strings returned by the AJAX to set the options of these dropdown menus to the respective options. Therefore I'm using these two jquery commands already displayed above:






$('#liebraum option:eq(data.raum)').prop('selected', true)
$('#liebsitz option:eq(data.nummer)').prop('selected', true)





Currently, nothing is happening ^^ Its like the two dropwdown menus are not even touched. I wonder if this is because I have to put these variables containing the strings to compare to into the " '' " clause. Because of this they are just regarded as strings, not as variables. I never had such a case so far in the context of jquery and I have no idea if there is a way to deal with it Oo










share|improve this question



























    -1















    I basically want to set some dropdown menu to a certain index by comparing to the respective value.
    Here's the code:






    function InitializeLieblingsplatzOptions(){
    $.post('../include/retrieveLieblingsplatz.php',{
    //nothing to transmit
    }).then((data) => {
    data = JSON.parse(data)
    console.log("data in retrieveLieblingsplatz.php is ", data)

    //let test = $('#liebraum').find("test")
    $('#liebraum option:eq(data.raum)').prop('selected', true)
    $('#liebsitz option:eq(data.nummer)').prop('selected', true)



    })
    }





    the data returned from the ajax contains two strings which represent a roomname and seatnumber. There are 2 dropdown menus on this site which contain all names of all rooms and all available seats of the respective room.
    I now want to use the 2 strings returned by the AJAX to set the options of these dropdown menus to the respective options. Therefore I'm using these two jquery commands already displayed above:






    $('#liebraum option:eq(data.raum)').prop('selected', true)
    $('#liebsitz option:eq(data.nummer)').prop('selected', true)





    Currently, nothing is happening ^^ Its like the two dropwdown menus are not even touched. I wonder if this is because I have to put these variables containing the strings to compare to into the " '' " clause. Because of this they are just regarded as strings, not as variables. I never had such a case so far in the context of jquery and I have no idea if there is a way to deal with it Oo










    share|improve this question

























      -1












      -1








      -1








      I basically want to set some dropdown menu to a certain index by comparing to the respective value.
      Here's the code:






      function InitializeLieblingsplatzOptions(){
      $.post('../include/retrieveLieblingsplatz.php',{
      //nothing to transmit
      }).then((data) => {
      data = JSON.parse(data)
      console.log("data in retrieveLieblingsplatz.php is ", data)

      //let test = $('#liebraum').find("test")
      $('#liebraum option:eq(data.raum)').prop('selected', true)
      $('#liebsitz option:eq(data.nummer)').prop('selected', true)



      })
      }





      the data returned from the ajax contains two strings which represent a roomname and seatnumber. There are 2 dropdown menus on this site which contain all names of all rooms and all available seats of the respective room.
      I now want to use the 2 strings returned by the AJAX to set the options of these dropdown menus to the respective options. Therefore I'm using these two jquery commands already displayed above:






      $('#liebraum option:eq(data.raum)').prop('selected', true)
      $('#liebsitz option:eq(data.nummer)').prop('selected', true)





      Currently, nothing is happening ^^ Its like the two dropwdown menus are not even touched. I wonder if this is because I have to put these variables containing the strings to compare to into the " '' " clause. Because of this they are just regarded as strings, not as variables. I never had such a case so far in the context of jquery and I have no idea if there is a way to deal with it Oo










      share|improve this question














      I basically want to set some dropdown menu to a certain index by comparing to the respective value.
      Here's the code:






      function InitializeLieblingsplatzOptions(){
      $.post('../include/retrieveLieblingsplatz.php',{
      //nothing to transmit
      }).then((data) => {
      data = JSON.parse(data)
      console.log("data in retrieveLieblingsplatz.php is ", data)

      //let test = $('#liebraum').find("test")
      $('#liebraum option:eq(data.raum)').prop('selected', true)
      $('#liebsitz option:eq(data.nummer)').prop('selected', true)



      })
      }





      the data returned from the ajax contains two strings which represent a roomname and seatnumber. There are 2 dropdown menus on this site which contain all names of all rooms and all available seats of the respective room.
      I now want to use the 2 strings returned by the AJAX to set the options of these dropdown menus to the respective options. Therefore I'm using these two jquery commands already displayed above:






      $('#liebraum option:eq(data.raum)').prop('selected', true)
      $('#liebsitz option:eq(data.nummer)').prop('selected', true)





      Currently, nothing is happening ^^ Its like the two dropwdown menus are not even touched. I wonder if this is because I have to put these variables containing the strings to compare to into the " '' " clause. Because of this they are just regarded as strings, not as variables. I never had such a case so far in the context of jquery and I have no idea if there is a way to deal with it Oo






      function InitializeLieblingsplatzOptions(){
      $.post('../include/retrieveLieblingsplatz.php',{
      //nothing to transmit
      }).then((data) => {
      data = JSON.parse(data)
      console.log("data in retrieveLieblingsplatz.php is ", data)

      //let test = $('#liebraum').find("test")
      $('#liebraum option:eq(data.raum)').prop('selected', true)
      $('#liebsitz option:eq(data.nummer)').prop('selected', true)



      })
      }





      function InitializeLieblingsplatzOptions(){
      $.post('../include/retrieveLieblingsplatz.php',{
      //nothing to transmit
      }).then((data) => {
      data = JSON.parse(data)
      console.log("data in retrieveLieblingsplatz.php is ", data)

      //let test = $('#liebraum').find("test")
      $('#liebraum option:eq(data.raum)').prop('selected', true)
      $('#liebsitz option:eq(data.nummer)').prop('selected', true)



      })
      }





      $('#liebraum option:eq(data.raum)').prop('selected', true)
      $('#liebsitz option:eq(data.nummer)').prop('selected', true)





      $('#liebraum option:eq(data.raum)').prop('selected', true)
      $('#liebsitz option:eq(data.nummer)').prop('selected', true)






      jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 10:12









      JSONBUG123JSONBUG123

      6110




      6110
























          3 Answers
          3






          active

          oldest

          votes


















          0














          You can do it by using the css selector like so



          $('#liebraum option:eq('+data.raum+')').prop('selected', true);


          or by using the .eq method exposed by jQuery.



          $('#liebraum option').eq(data.raum).prop('selected', true);





          share|improve this answer































            1














            You can use ES6 template literals:



            $(`#liebraum option:eq(${data.raum})`)


            Or, if you want to stick to ES5, use String concatenation:



            $('#liebraum option:eq('+data.raum+')')





            share|improve this answer































              0














              Please search how to concatenate strings in javascript...



              $('#yourID option:eq(' + yourVariable + ')')





              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%2f53317035%2fhow-to-use-a-variable-inside-id-optioneqvariable%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                You can do it by using the css selector like so



                $('#liebraum option:eq('+data.raum+')').prop('selected', true);


                or by using the .eq method exposed by jQuery.



                $('#liebraum option').eq(data.raum).prop('selected', true);





                share|improve this answer




























                  0














                  You can do it by using the css selector like so



                  $('#liebraum option:eq('+data.raum+')').prop('selected', true);


                  or by using the .eq method exposed by jQuery.



                  $('#liebraum option').eq(data.raum).prop('selected', true);





                  share|improve this answer


























                    0












                    0








                    0







                    You can do it by using the css selector like so



                    $('#liebraum option:eq('+data.raum+')').prop('selected', true);


                    or by using the .eq method exposed by jQuery.



                    $('#liebraum option').eq(data.raum).prop('selected', true);





                    share|improve this answer













                    You can do it by using the css selector like so



                    $('#liebraum option:eq('+data.raum+')').prop('selected', true);


                    or by using the .eq method exposed by jQuery.



                    $('#liebraum option').eq(data.raum).prop('selected', true);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 10:19









                    Miguel M.Miguel M.

                    367312




                    367312

























                        1














                        You can use ES6 template literals:



                        $(`#liebraum option:eq(${data.raum})`)


                        Or, if you want to stick to ES5, use String concatenation:



                        $('#liebraum option:eq('+data.raum+')')





                        share|improve this answer




























                          1














                          You can use ES6 template literals:



                          $(`#liebraum option:eq(${data.raum})`)


                          Or, if you want to stick to ES5, use String concatenation:



                          $('#liebraum option:eq('+data.raum+')')





                          share|improve this answer


























                            1












                            1








                            1







                            You can use ES6 template literals:



                            $(`#liebraum option:eq(${data.raum})`)


                            Or, if you want to stick to ES5, use String concatenation:



                            $('#liebraum option:eq('+data.raum+')')





                            share|improve this answer













                            You can use ES6 template literals:



                            $(`#liebraum option:eq(${data.raum})`)


                            Or, if you want to stick to ES5, use String concatenation:



                            $('#liebraum option:eq('+data.raum+')')






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 10:16









                            connexoconnexo

                            22.5k83561




                            22.5k83561























                                0














                                Please search how to concatenate strings in javascript...



                                $('#yourID option:eq(' + yourVariable + ')')





                                share|improve this answer




























                                  0














                                  Please search how to concatenate strings in javascript...



                                  $('#yourID option:eq(' + yourVariable + ')')





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Please search how to concatenate strings in javascript...



                                    $('#yourID option:eq(' + yourVariable + ')')





                                    share|improve this answer













                                    Please search how to concatenate strings in javascript...



                                    $('#yourID option:eq(' + yourVariable + ')')






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 15 '18 at 10:16









                                    Jean-Marc ZimmerJean-Marc Zimmer

                                    38414




                                    38414






























                                        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%2f53317035%2fhow-to-use-a-variable-inside-id-optioneqvariable%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

                                        Florida Star v. B. J. F.

                                        Danny Elfman

                                        Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues