javascript - How to print the content of a div that changes based on a foreach statement? -


i have arraylist named conversations_client, want able value of conversation[6] each div. each 1 the media div represent conversation. here part of code :

<c:foreach var="conversation" items="${conversations_client}" >     <div class="media">         <div class="media-left">             <input class="checkbox" type="checkbox" name="selected-conv">         </div>     <div class="media-body">          <h4 class="media-heading">${conversation[0]}</h4>          <span class="hidden">${conversation[6]}</span>  ...... 

i had first tried use code :

<script type="text/javascript">     $('.media').click(function(){        var text = $(this).text();        alert(text);     });      </script> 

but print not conversation[6] others since they're inside div class media.

then tried :

<script type="text/javascript">     $('.media').click(function(){        var text = $('.hidden').text();        alert(text);     });              </script> 

but print same id divs, id of first one.

how can that? , better wrap media divs tags able use specific action each 1 of them? because displaying conversations, once user click on 1 them i'll have recover id able display messages of specific conversation , isn't advisable write java code inside of jsp.

thank :)

use find current element.

$('.media').click(function()     {         var text = $(this).find('#hidden').text();         alert(text);    }); 

update:

you using loop repeat markup. it's not use id hidden multiple times per w3c standards. id should unique , should used once in page.

use class instead of id multiple usages. in case class="hidden"

better change <span id="hidden">${conversation[6]}</span> <span class="hidden">${conversation[6]}</span>

also change js find .hidden

 $('.media').click(function()     {         var text = $(this).find('.hidden').text();         alert(text);    }); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -