using jQuery attr('onclick') returns undefined -
i'm trying access 'onclick' attribute of dynamically generated link:
<div class="idx-linkvirtualtour"> <p class="detailslink_p">virtual tour</p> <div class="idx-detailssublink"> <a onclick="window.open('http://www.tourfactory.com/736220', 'virtualtour', 'width=800, height=600, resizable=1, scrollbars=1, status=0, titlebar=0')" href="javascript:void(0)">virtual tour</a> </div> </div>
element appears in firebug ^
can access href attribute by:
var $sublinka = $('.idx-detailssublink a'); log( $sublinka[0].href + "\n" + $sublinka[0].onclick); // output value firebug console
output firebug console:
http://www.vdbestates.com/... (intentionally shortened)
undefined
any ideas?? thanks!
(edit) answered:
var $sublinka = $('.idx-detailssublink a')
refers multiple elements, although not each of these element's tags have onclick attribute. realized, 1 of elements have it. restructured jquery such:
var $virtuallink = $('.idx-linkvirtualtour .idx-detailssublink > a'); log("onclick: " + $virtuallink.attr('onclick'));
and obtained value of onclick attribute. moral of story, careful trying access attributes of set of elements, because may not have desired attribute, hence returning undefined.
cheers!
considering href
console message different in html, i'd element @ index [0]
different 1 you're targeting.
if have multiple instances of .idx-detailssublink a
, you're grabbing first.
if have 1 on page, returns function wraps content of inline onclick
.
if want actual attribute, can use getattribute()
instead.
var $sublinka = $('.idx-detailssublink a'); log( $sublinka[0].href + "\n" + $sublinka[0].getattribute('onclick'));
but still need referencing correct element.
Comments
Post a Comment