0

I'm trying to iterate through a series of DIVs, like so:

$(".div_class").each(() => {
  console.log($(this))

  // something happens here
})

However, the $(this) being logged to the console is not the DOM element I'm trying to access. Instead, I'm getting a load of socket.io information:

S.fn.init [Socket]
  0: Socket {receiveBuffer: Array(0), sendBuffer: Array(0), ids: 0, acks: 
  {…}, flags: {…}, …}
  length: 1
  [[Prototype]]: Object(0)

(and so on and so forth)

Does anyone know why this might be, and how I might be able to get the properties of the elements I'm looping through/trying to access?

Tom Davies
  • 151
  • 1
  • 8
  • 2
    When using arrow syntax, "this" is the outer context, not the inner context. You have nothing in your each parameter: `.each((element) => { })`. – Daniel W. Dec 14 '21 at 14:12
  • 2
    Because fat arrows work like that. function() } and () => {} are not really interchangeable. They behave differently which you just found out. – epascarello Dec 14 '21 at 14:12
  • That makes a lot of sense - appreciate it. – Tom Davies Dec 14 '21 at 14:52

0 Answers0