alert - How to get the content of a hidden sibling-div to a button - JQuery related -
maybe i've searched poorly, far have not found answer question. first code of javascript function:
<script> function showcomment(){ var $data = $(this).parent("td").contents("div.hiddencomment").text(); console.log($data); alert($data); return false; } </script>
i've included the html code i'm working on below. <table>
, , in 1 <td>
there <button>
, hidden <div>
. content of <div>
should presented in alert/dialog box.
<table class="center"> <thead> <tr> <th>status</th> <th>datum</th> <th>zeit</th> <th>amount</th> <th>source</th> <th colspan="2">comment</th> </tr> </thead> <tbody> <tr> <td>status1</td> <td>2013-04-04</td> <td>06:30:38</td> <td>3.646.268,00</td> <td>src1</td> <td> <div class="hiddencomment">a comment</div> <button name="showcomment" type="button" href="#" class="showcomment" onclick="showcomment()">show</button> </td> <td><a href="#" class="addcomment">add</a> </td> </tr> <tr> <td>status</td> <td>2013-04-05</td> <td>06:30:48</td> <td>1.732.213,00</td> <td>src2</td> <td> <div class="hiddencomment">an other comment</div> <button name="showcomment" type="button" href="#" class="showcomment" onclick="showcomment()">show</button> </td> <td><a href="#" class="addcomment">add</a> </td> </tr> ..... </body> </table>
i guess can idea <table>
code. anyhow, best achieved until after searching web "undefined"
message.
what should note: class .hiddencomment
has css property display:none
.
any hints, tips , tricks warmly welcome!
thanks time , help.
you can attach click event class showcomment. there can previous element , it's text.
$('.showcomment').click(function(){ alert($(this).prev().text()); //or below if order of elements might change. //alert($(this).siblings('.hiddencomment').text()); });
if of content dynamicly loaded can use delegate:
$('body').on('click','.showcomment',function(){ alert($(this).prev().text()); });
Comments
Post a Comment