javascript - How to get the value of an element by traversing to parent div -
i working on code have multiple div's generated dynamically. problem current hidden elements value on click of link particular div(e.g when click on add favorites should alert value of task_for hidden input). code working fine 1 div if there multiple divs alerts first element's value. here code-
<div class="col-md-4 col-sm-4 col-xs-12"> <div class="search_box_list"> <a href="javascript:void(0)" class="add_fav"><i class="fa fa-star-o" aria-hidden="true"></i>add favourites</a> <div class="n_s">'+value.user_name+'</div><div class="n_e">'+value.user_email+'</div> <div class="n_n">'+value.user_phone+'</div> <div class="n_d">'+value.user_designation+'</div><div class="n_d"> <button type="button" id="assign" class="assign_task" class="btn btn-default"> <input type="hidden" name="for_id" class="task_for" value="'+value.user_id+'" /> <input type="hidden" name="for_id" class="email" value="'+value.user_email+'" /> assign task </button> </div> </div> </div>
and jquery
$("body").on('click','.add_fav',function(){ var who_id=<?php echo $_session['global']['uid'] ?>; var whom_id=$(document).find(".task_for").val(); alert(whom_id) });
use closest parent .search_box_list , use find value of hidden div
$("body").on('click','.add_fav',function(){ var who_id = <?php echo $_session['global']['uid'] ?>; var whom_id = $(this).closest('.search_box_list').find(".task_for").val(); alert(whom_id) });
Comments
Post a Comment