0

The DOM is something like this:

<h3 class="iw"><span email="noreply@updates.com" name="test.com" class="gD">test.com</span></h3>

Now I want to get the email attribute of the span.

I tried following in console log.

console.log($(".iw span:first-child"));

I get the complete inner span element and its other line as undefined on the console.

Now if I try following on console log,

console.log($(".iw span:first-child").attr("email"));

It starts giving me this error:

Uncaught TypeError: $(...).attr is not a function(…)

It gives the same error for any jQuery method that I may use after that.

Image - Error coneol log

How can I fix this problem?

Kaushik Ray
  • 555
  • 2
  • 8
  • 21

2 Answers2

0

This works completely fine, you just need to make sure you're including the jQuery library before trying to console.log the email attribute.

Place this in the <head> of your HTML :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

So then calling console.log($(".iw span:first-child").attr("email")); will return 'noreply@updates.com'.

aphextwix
  • 1,838
  • 3
  • 21
  • 27
0

The browser console comes with a function $ that acts like jQuery but isn't. Said function is only available while on the browser console and not usable in scripts included in your page

Like aphextwix said in his answer, you should be fine if you include jQuery in your page like:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

If you are absolutely sure you are including jQuery but still can't debug in the console with the $() try using jQuery() like so:

console.log(jQuery(".iw span:first-child").attr("email"));
JSelser
  • 3,510
  • 1
  • 19
  • 40