jQuery - Find .attr of the last-child element -


i'm trying jquery .attr("msg_g") of last-child element

<div id="append">   <div class="h" msg_g="1">a</div>   <div class="y" msg_g="2">b</div>   <div class="h" msg_g="3">c</div> </div> 

i'm pretty sure script below must work returns me undefined

var msg_g = $( "#append div:last-child" ).attr( "msg_g" ); console.log(msg_g); 

i use script below checking if last-child 'hasclass' , works perfect, why doesn't work on .attr()

example .hasclass()

if($( "#append div:last-child" ).hasclass( "y" )){     var last_msg_class="by_you"; }else if($( "#append div:last-child" ).hasclass( "h" )){     var last_msg_class="by_friend"; } 

instead of adding msg_g attribute,try using data-* attribute.

modify html :--

<div id="append"> <div class="h" data-msg_g="1">a</div> <div class="y" data-msg_g="2">b</div> <div class="h" data-msg_g="3">c</div> </div> 

and try in jquery :--

var msg_g = $( "#append > div:last" ).data( "msg_g" ); 

and make sure you're jquery code inside document.ready block.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -