Krishna shidnekoppa Krishna shidnekoppa - 2 months ago 12
jQuery Question

How to get attribute value of span

On button click want the value from span

<td style="border-style:None;">
<span data-needed="4080">
<input id="MainContent_grdr1_chkCheck_0" type="checkbox" name="ctl00$MainContent$grd2$ctl02$chkCheck"></span>
<a id="MainContent_grdr1_lnkViewLog_0" class="btn btn-primary btn-xs btn-graph" href="javascript:__doPostBack('ctl00$MainContent$grdRatesAcknowledge$ctl02$lnkViewLog','')"><i class="fa fa-line-chart" aria-hidden="true"></i></a>
</td>


Tried with this code getting [object] as alert.

<script>
$('.btn-graph').on('click', function () {
var proid = $(this).closest('span').find('data-needed');
alert(proid);
});
</script>

Answer

Use attr()

$('.btn-graph').on('click', function (e) {
     e.preventDefult();
     var proid = $(this).prev('span').attr('data-needed');
     alert(proid);
     __doPostBack('ctl00$MainContent$grdRatesAcknowledge$ctl02$lnkViewLog','')
}