Get variable inside certain div with jquery, when there are multiple divs with same id












2














I'm printing an arraylist with jsp. Each object inside of that arraylist is printed with a loop like this:



                        <% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>


<%int index = 0;%>
<%for(MessageObject msg :list){
index++;
if(mensaje.getState().compareTo("unread") == 0){%>
<tr data-status="unread" class="unread">
<td>
<a href="javascript:;" class="star">
<i class="glyphicon glyphicon-star"></i>
</a>
</td>
<td>
<div class="media">
<h4 class="title">
User Identifier
</h4>
</div>
</td>
<td id="unread-id">
<div class="media">
<p class="summary"><% out.print(msg.getMessage());%></p>

<input id="index" type="text" value="<%out.print(index);%>"></input>

</div>


Some of the closing tags and other structures are not written above, in order to make my code easier to read.

Basically that prints me messages from a queue, and its index in the arraylist:
enter image description here



My problem is that I want to save the index value of any of my messages when I click on them.



I tried this:



<script>
$(document).on('click', '#unread-id', function () {
var index = $('#index').val();
$("#setindex").val(index);
});




So I click on any div containing a message, the script is called, but I always get the same index value, 1.
Problem is that having always the same div with the same id name, causes that my script always selects the first div with id unread-id, which is always the first one, so it returns 1.



How can I get the index of the clicked div, if all my container divs have the same id value?










share|improve this question



























    2














    I'm printing an arraylist with jsp. Each object inside of that arraylist is printed with a loop like this:



                            <% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>


    <%int index = 0;%>
    <%for(MessageObject msg :list){
    index++;
    if(mensaje.getState().compareTo("unread") == 0){%>
    <tr data-status="unread" class="unread">
    <td>
    <a href="javascript:;" class="star">
    <i class="glyphicon glyphicon-star"></i>
    </a>
    </td>
    <td>
    <div class="media">
    <h4 class="title">
    User Identifier
    </h4>
    </div>
    </td>
    <td id="unread-id">
    <div class="media">
    <p class="summary"><% out.print(msg.getMessage());%></p>

    <input id="index" type="text" value="<%out.print(index);%>"></input>

    </div>


    Some of the closing tags and other structures are not written above, in order to make my code easier to read.

    Basically that prints me messages from a queue, and its index in the arraylist:
    enter image description here



    My problem is that I want to save the index value of any of my messages when I click on them.



    I tried this:



    <script>
    $(document).on('click', '#unread-id', function () {
    var index = $('#index').val();
    $("#setindex").val(index);
    });




    So I click on any div containing a message, the script is called, but I always get the same index value, 1.
    Problem is that having always the same div with the same id name, causes that my script always selects the first div with id unread-id, which is always the first one, so it returns 1.



    How can I get the index of the clicked div, if all my container divs have the same id value?










    share|improve this question

























      2












      2








      2







      I'm printing an arraylist with jsp. Each object inside of that arraylist is printed with a loop like this:



                              <% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>


      <%int index = 0;%>
      <%for(MessageObject msg :list){
      index++;
      if(mensaje.getState().compareTo("unread") == 0){%>
      <tr data-status="unread" class="unread">
      <td>
      <a href="javascript:;" class="star">
      <i class="glyphicon glyphicon-star"></i>
      </a>
      </td>
      <td>
      <div class="media">
      <h4 class="title">
      User Identifier
      </h4>
      </div>
      </td>
      <td id="unread-id">
      <div class="media">
      <p class="summary"><% out.print(msg.getMessage());%></p>

      <input id="index" type="text" value="<%out.print(index);%>"></input>

      </div>


      Some of the closing tags and other structures are not written above, in order to make my code easier to read.

      Basically that prints me messages from a queue, and its index in the arraylist:
      enter image description here



      My problem is that I want to save the index value of any of my messages when I click on them.



      I tried this:



      <script>
      $(document).on('click', '#unread-id', function () {
      var index = $('#index').val();
      $("#setindex").val(index);
      });




      So I click on any div containing a message, the script is called, but I always get the same index value, 1.
      Problem is that having always the same div with the same id name, causes that my script always selects the first div with id unread-id, which is always the first one, so it returns 1.



      How can I get the index of the clicked div, if all my container divs have the same id value?










      share|improve this question













      I'm printing an arraylist with jsp. Each object inside of that arraylist is printed with a loop like this:



                              <% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>


      <%int index = 0;%>
      <%for(MessageObject msg :list){
      index++;
      if(mensaje.getState().compareTo("unread") == 0){%>
      <tr data-status="unread" class="unread">
      <td>
      <a href="javascript:;" class="star">
      <i class="glyphicon glyphicon-star"></i>
      </a>
      </td>
      <td>
      <div class="media">
      <h4 class="title">
      User Identifier
      </h4>
      </div>
      </td>
      <td id="unread-id">
      <div class="media">
      <p class="summary"><% out.print(msg.getMessage());%></p>

      <input id="index" type="text" value="<%out.print(index);%>"></input>

      </div>


      Some of the closing tags and other structures are not written above, in order to make my code easier to read.

      Basically that prints me messages from a queue, and its index in the arraylist:
      enter image description here



      My problem is that I want to save the index value of any of my messages when I click on them.



      I tried this:



      <script>
      $(document).on('click', '#unread-id', function () {
      var index = $('#index').val();
      $("#setindex").val(index);
      });




      So I click on any div containing a message, the script is called, but I always get the same index value, 1.
      Problem is that having always the same div with the same id name, causes that my script always selects the first div with id unread-id, which is always the first one, so it returns 1.



      How can I get the index of the clicked div, if all my container divs have the same id value?







      javascript java jquery spring jsp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 22:41









      Keka Bron

      919




      919
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Add a class to your <td id="unread-id"> like row and change your script for the following one. Your td should end up looking like <td class="row">. Also, don't use ids in your inputs, change it to a class, like row-input.



          JS



          $(document).on('click', '.row', function () {
          var index = $(this).find('.row-input').val();
          $("#setindex").val(index);
          });


          JSP Changes





          • <td id="unread-id"> to <td class="row">


          • <input id="index" type="text" value="<%out.print(index);%>"></input> to <input class="row-input" type="text" value="<%out.print(index);%>"></input>


          Note



          You are setting the same id to all your rows. An id must be unique and that is the reason you keep getting the same index.






          share|improve this answer































            1














            First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).



            However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):






            $(function() {
            $(document).on('click', '#unread-id', function () {
            console.log($(this).val());
            });
            });

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <input id="unread-id" value="1" /><br />
            <input id="unread-id" value="2" /><br />
            <input id="unread-id" value="3" /><br />
            <input id="unread-id" value="4" /><br />





            When inside the click function - the this element is the element you just clicked. You can use that in order to get the value that you need.






            share|improve this answer





















            • Well, id should be unique. That is already a bit of an issue.
              – Alain Cruz
              Nov 13 '18 at 0:31






            • 1




              @AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
              – Dekel
              Nov 13 '18 at 0:33










            • and btw - your usage if $(this) after you read my answer is not so nice :)
              – Dekel
              Nov 13 '18 at 0:36












            • You know, I only saw the first duplicated id in his td, until I realized there was another in his input hahaha :-)
              – Alain Cruz
              Nov 13 '18 at 0:37






            • 1




              Well I am upvoting, because you did show it. Still, yours is missing the td's id issue, which he/she should fix.
              – Alain Cruz
              Nov 13 '18 at 0:40













            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%2f53271142%2fget-variable-inside-certain-div-with-jquery-when-there-are-multiple-divs-with-s%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









            2














            Add a class to your <td id="unread-id"> like row and change your script for the following one. Your td should end up looking like <td class="row">. Also, don't use ids in your inputs, change it to a class, like row-input.



            JS



            $(document).on('click', '.row', function () {
            var index = $(this).find('.row-input').val();
            $("#setindex").val(index);
            });


            JSP Changes





            • <td id="unread-id"> to <td class="row">


            • <input id="index" type="text" value="<%out.print(index);%>"></input> to <input class="row-input" type="text" value="<%out.print(index);%>"></input>


            Note



            You are setting the same id to all your rows. An id must be unique and that is the reason you keep getting the same index.






            share|improve this answer




























              2














              Add a class to your <td id="unread-id"> like row and change your script for the following one. Your td should end up looking like <td class="row">. Also, don't use ids in your inputs, change it to a class, like row-input.



              JS



              $(document).on('click', '.row', function () {
              var index = $(this).find('.row-input').val();
              $("#setindex").val(index);
              });


              JSP Changes





              • <td id="unread-id"> to <td class="row">


              • <input id="index" type="text" value="<%out.print(index);%>"></input> to <input class="row-input" type="text" value="<%out.print(index);%>"></input>


              Note



              You are setting the same id to all your rows. An id must be unique and that is the reason you keep getting the same index.






              share|improve this answer


























                2












                2








                2






                Add a class to your <td id="unread-id"> like row and change your script for the following one. Your td should end up looking like <td class="row">. Also, don't use ids in your inputs, change it to a class, like row-input.



                JS



                $(document).on('click', '.row', function () {
                var index = $(this).find('.row-input').val();
                $("#setindex").val(index);
                });


                JSP Changes





                • <td id="unread-id"> to <td class="row">


                • <input id="index" type="text" value="<%out.print(index);%>"></input> to <input class="row-input" type="text" value="<%out.print(index);%>"></input>


                Note



                You are setting the same id to all your rows. An id must be unique and that is the reason you keep getting the same index.






                share|improve this answer














                Add a class to your <td id="unread-id"> like row and change your script for the following one. Your td should end up looking like <td class="row">. Also, don't use ids in your inputs, change it to a class, like row-input.



                JS



                $(document).on('click', '.row', function () {
                var index = $(this).find('.row-input').val();
                $("#setindex").val(index);
                });


                JSP Changes





                • <td id="unread-id"> to <td class="row">


                • <input id="index" type="text" value="<%out.print(index);%>"></input> to <input class="row-input" type="text" value="<%out.print(index);%>"></input>


                Note



                You are setting the same id to all your rows. An id must be unique and that is the reason you keep getting the same index.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 0:35

























                answered Nov 13 '18 at 0:29









                Alain Cruz

                1,7381818




                1,7381818

























                    1














                    First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).



                    However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):






                    $(function() {
                    $(document).on('click', '#unread-id', function () {
                    console.log($(this).val());
                    });
                    });

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <input id="unread-id" value="1" /><br />
                    <input id="unread-id" value="2" /><br />
                    <input id="unread-id" value="3" /><br />
                    <input id="unread-id" value="4" /><br />





                    When inside the click function - the this element is the element you just clicked. You can use that in order to get the value that you need.






                    share|improve this answer





















                    • Well, id should be unique. That is already a bit of an issue.
                      – Alain Cruz
                      Nov 13 '18 at 0:31






                    • 1




                      @AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
                      – Dekel
                      Nov 13 '18 at 0:33










                    • and btw - your usage if $(this) after you read my answer is not so nice :)
                      – Dekel
                      Nov 13 '18 at 0:36












                    • You know, I only saw the first duplicated id in his td, until I realized there was another in his input hahaha :-)
                      – Alain Cruz
                      Nov 13 '18 at 0:37






                    • 1




                      Well I am upvoting, because you did show it. Still, yours is missing the td's id issue, which he/she should fix.
                      – Alain Cruz
                      Nov 13 '18 at 0:40


















                    1














                    First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).



                    However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):






                    $(function() {
                    $(document).on('click', '#unread-id', function () {
                    console.log($(this).val());
                    });
                    });

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <input id="unread-id" value="1" /><br />
                    <input id="unread-id" value="2" /><br />
                    <input id="unread-id" value="3" /><br />
                    <input id="unread-id" value="4" /><br />





                    When inside the click function - the this element is the element you just clicked. You can use that in order to get the value that you need.






                    share|improve this answer





















                    • Well, id should be unique. That is already a bit of an issue.
                      – Alain Cruz
                      Nov 13 '18 at 0:31






                    • 1




                      @AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
                      – Dekel
                      Nov 13 '18 at 0:33










                    • and btw - your usage if $(this) after you read my answer is not so nice :)
                      – Dekel
                      Nov 13 '18 at 0:36












                    • You know, I only saw the first duplicated id in his td, until I realized there was another in his input hahaha :-)
                      – Alain Cruz
                      Nov 13 '18 at 0:37






                    • 1




                      Well I am upvoting, because you did show it. Still, yours is missing the td's id issue, which he/she should fix.
                      – Alain Cruz
                      Nov 13 '18 at 0:40
















                    1












                    1








                    1






                    First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).



                    However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):






                    $(function() {
                    $(document).on('click', '#unread-id', function () {
                    console.log($(this).val());
                    });
                    });

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <input id="unread-id" value="1" /><br />
                    <input id="unread-id" value="2" /><br />
                    <input id="unread-id" value="3" /><br />
                    <input id="unread-id" value="4" /><br />





                    When inside the click function - the this element is the element you just clicked. You can use that in order to get the value that you need.






                    share|improve this answer












                    First - id should be unique in your page. You should really fix this (and if you need some selector to work with multiple elements - you can use classname instead).



                    However - your code can work (might cause issues with some browsers, so I really advise you to fix this asap):






                    $(function() {
                    $(document).on('click', '#unread-id', function () {
                    console.log($(this).val());
                    });
                    });

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <input id="unread-id" value="1" /><br />
                    <input id="unread-id" value="2" /><br />
                    <input id="unread-id" value="3" /><br />
                    <input id="unread-id" value="4" /><br />





                    When inside the click function - the this element is the element you just clicked. You can use that in order to get the value that you need.






                    $(function() {
                    $(document).on('click', '#unread-id', function () {
                    console.log($(this).val());
                    });
                    });

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <input id="unread-id" value="1" /><br />
                    <input id="unread-id" value="2" /><br />
                    <input id="unread-id" value="3" /><br />
                    <input id="unread-id" value="4" /><br />





                    $(function() {
                    $(document).on('click', '#unread-id', function () {
                    console.log($(this).val());
                    });
                    });

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <input id="unread-id" value="1" /><br />
                    <input id="unread-id" value="2" /><br />
                    <input id="unread-id" value="3" /><br />
                    <input id="unread-id" value="4" /><br />






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 0:30









                    Dekel

                    42.5k54567




                    42.5k54567












                    • Well, id should be unique. That is already a bit of an issue.
                      – Alain Cruz
                      Nov 13 '18 at 0:31






                    • 1




                      @AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
                      – Dekel
                      Nov 13 '18 at 0:33










                    • and btw - your usage if $(this) after you read my answer is not so nice :)
                      – Dekel
                      Nov 13 '18 at 0:36












                    • You know, I only saw the first duplicated id in his td, until I realized there was another in his input hahaha :-)
                      – Alain Cruz
                      Nov 13 '18 at 0:37






                    • 1




                      Well I am upvoting, because you did show it. Still, yours is missing the td's id issue, which he/she should fix.
                      – Alain Cruz
                      Nov 13 '18 at 0:40




















                    • Well, id should be unique. That is already a bit of an issue.
                      – Alain Cruz
                      Nov 13 '18 at 0:31






                    • 1




                      @AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
                      – Dekel
                      Nov 13 '18 at 0:33










                    • and btw - your usage if $(this) after you read my answer is not so nice :)
                      – Dekel
                      Nov 13 '18 at 0:36












                    • You know, I only saw the first duplicated id in his td, until I realized there was another in his input hahaha :-)
                      – Alain Cruz
                      Nov 13 '18 at 0:37






                    • 1




                      Well I am upvoting, because you did show it. Still, yours is missing the td's id issue, which he/she should fix.
                      – Alain Cruz
                      Nov 13 '18 at 0:40


















                    Well, id should be unique. That is already a bit of an issue.
                    – Alain Cruz
                    Nov 13 '18 at 0:31




                    Well, id should be unique. That is already a bit of an issue.
                    – Alain Cruz
                    Nov 13 '18 at 0:31




                    1




                    1




                    @AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
                    – Dekel
                    Nov 13 '18 at 0:33




                    @AlainCruz completely agree with you, and it's mentioned in my answer. However - even in that case - this will work (in current browsers). The solution might not work in the future, so I really advise to use class instead of duplicate the id all over the page.
                    – Dekel
                    Nov 13 '18 at 0:33












                    and btw - your usage if $(this) after you read my answer is not so nice :)
                    – Dekel
                    Nov 13 '18 at 0:36






                    and btw - your usage if $(this) after you read my answer is not so nice :)
                    – Dekel
                    Nov 13 '18 at 0:36














                    You know, I only saw the first duplicated id in his td, until I realized there was another in his input hahaha :-)
                    – Alain Cruz
                    Nov 13 '18 at 0:37




                    You know, I only saw the first duplicated id in his td, until I realized there was another in his input hahaha :-)
                    – Alain Cruz
                    Nov 13 '18 at 0:37




                    1




                    1




                    Well I am upvoting, because you did show it. Still, yours is missing the td's id issue, which he/she should fix.
                    – Alain Cruz
                    Nov 13 '18 at 0:40






                    Well I am upvoting, because you did show it. Still, yours is missing the td's id issue, which he/she should fix.
                    – Alain Cruz
                    Nov 13 '18 at 0:40




















                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53271142%2fget-variable-inside-certain-div-with-jquery-when-there-are-multiple-divs-with-s%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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python