javascript - jQuery .attr returns undefined -


i have html element .replylink , input field type hidden has value (in case 462). want able value of value attribute of input .hddnscrapid when click on .replylink element. html:

<div class="scrapitemparent">     <input class="hddnscrapid" type="hidden" value="462"/>     <img class="scrapprofilepic" src=" static/img/user/personalprofilepicture/mod_50_50/150972db1a0c9e863746c12797398b6e40ae05c8.jpg" />     <div class="scrapcontent"><br />         <video class="scrapvideo" controls>             <source src="../../../scrapll_m/static/vid/4045e7944d8ea8f10dd4826a1e1595a7cef73b0c.mp4" type="video/mp4">         </video><br />         <span class="scraptime">2014-05-27 16:51:22<br />erol simsir             <a class="replylink" href="javascript:void(0);">reply</a>             <a class="replyclose" href="javascript:void(0);">x</a>         </span>     </div> </div> 

this js have now:

$('.replylink').click(function(){      var hddnscrapid = $(this).closest(".hddnscrapid").attr("value");     alert(hddnscrapid); }); 

this keeps saying 'undefined'. guess problem occurs because closest() function can't find input field hddnscrapid. why doesn't work?

1) closest looks among element , ancestors , .hddnscrapid element isn't parent of clicked element.

2) uptodate value, must use val, not attr("value").

you want :

var hddnscrapid = $(this).closest(".scrapitemparent").find(".hddnscrapid").val(); 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -