coffeescript - JQuery attr() on div not returning value -
i'm working backbone app written in coffeescript. i'm trying access cid associated each div in list of divs. each div has same 'contact' class , cid associated integer value.
<div class="contact-list"> <div class="contact" cid="1"></div> <div class="contact" cid="2"></div> <div class="contact" cid="3"></div> <div class="contact" cid="4"></div> <div class="contact" cid="5"></div> </div>
i want find element cid of 4:
console.log "all", $('.contact') div in $('.contact') console.log div console.log div.attr('cid')
the loop appears break upon calling .attr(). (when comment out line, code prints 5 divs. otherwise prints first) suggests must calling .attr() incorrectly?
when iterating on jquery object element reference referring dom element not jquery object can't access jquery methods directly on it
console.log "all", $('.contact') div in $('.contact') console.log $(div) console.log $(div).attr('cid')
use .each() like
console.log "all", $('.contact') $('.contact').each (i, div) -> console.log div console.log $(div).attr('cid')
demo: fiddle
Comments
Post a Comment