OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why does .attr() work on $(this) but not on parameter from each()?

  • Thread starter Thread starter user3665080
  • Start date Start date
U

user3665080

Guest
I am using cheerio (i.e jQuery server side) on node.js to parse a web page. When trying to get the href attribute of an element I am getting the following error:

TypeError: Object # has no method 'attr'

My code is:

Code:
$('#commandes_pagination li a').each(function(index, element) {
    console.log(element.attr('href'));
});

The HTML is like this:

Code:
<ul class="liste04" id="commandes_pagination">
    <li>        
        <a shape="rect" title="" href="/refonte/account/ajax/orderlist/1" class="page_num page_active" id="commandes_page-1">1</a>
    </li>
    <li>
        <a shape="rect" title="" href="/refonte/account/ajax/orderlist/2" class="page_num" id="commandes_page-2">2</a>
    </li>
</ul>

I can get it working by using $(this).attr("href") instead of element.attr("href") but I can't figure out why. Any explanation much appreciated!

<p>I am using cheerio (i.e jQuery server side) on node.js to parse a web page.
When trying to get the <code>href</code> attribute of an element I am getting the following error:</p>

<blockquote>
<p>TypeError: Object # has no method 'attr'</p>
</blockquote>

<p>My code is:</p>

<pre><code>$('#commandes_pagination li a').each(function(index, element) {
console.log(element.attr('href'));
});
</code></pre>

<p>The HTML is like this:</p>

<pre><code><ul class="liste04" id="commandes_pagination">
<li>
<a shape="rect" title="" href="/refonte/account/ajax/orderlist/1" class="page_num page_active" id="commandes_page-1">1</a>
</li>
<li>
<a shape="rect" title="" href="/refonte/account/ajax/orderlist/2" class="page_num" id="commandes_page-2">2</a>
</li>
</ul>
</code></pre>

<p>I can get it working by using <code>$(this).attr("href")</code> instead of <code>element.attr("href")</code> but I can't figure out why. Any explanation much appreciated!</p>
 
Top