Note that there are some explanatory texts on larger screens.

plurals
  1. POusing jQuery attr('onclick') returns undefined
    primarykey
    data
    text
    <p>I'm trying to access the 'onclick' attribute of a dynamically generated link:</p> <pre><code>&lt;div class="IDX-linkVirtualTour"&gt; &lt;p class="detailsLink_p"&gt;Virtual Tour&lt;/p&gt; &lt;div class="IDX-detailsSubLink"&gt; &lt;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)"&gt;Virtual Tour&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Element as it appears in firebug ^ <br /><br /> I can access the href attribute by:</p> <pre><code>var $sublinkA = $('.IDX-detailsSubLink a'); log( $sublinkA[0].href + "\n" + $sublinkA[0].onclick); // output the value to firebug console </code></pre> <p>Output to firebug console:<br /> <a href="http://www.vdbestates.com/" rel="nofollow">http://www.vdbestates.com/</a>... (intentionally shortened)<br /> undefined</p> <p>Any ideas?? Thanks! <br /> <br /> <hr /> (EDIT) ANSWERED:</p> <pre><code>var $sublinkA = $('.IDX-detailsSubLink a') </code></pre> <p>refers to multiple elements, although not each of these element's tags have the onclick attribute. I realized, only one of the elements have it. I restructured the jQuery as such:</p> <pre><code>var $virtualLink = $('.IDX-linkVirtualTour .IDX-detailsSubLink &gt; a'); log("onclick: " + $virtualLink.attr('onclick')); </code></pre> <p>and obtained the value of the onclick attribute. Moral of the story, be careful trying to access attributes of a set of elements, because they may not all have the desired attribute, hence returning undefined.</p> <p>Cheers!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload