0

I'm trying to get each element of my dynamically created form, and when I try to get it val or html it returns undefined

$('.articulo-row').each(_ => {
    $this = $(this)

    var a = $this.html()
    console.log(a)

})

.articulo-row should get all the <tr> rows that the user created.

This code gives me this error Cannot read property 'createDocumentFragment' of undefined I know that this error, it's because my object it's undefined. But if I print $(this) it returns a jQuery Object

Eduardo Iglesias
  • 1,056
  • 18
  • 42
  • It's not `$(this)` that is undefined. It is due to what is included in the initial selector you used. You can read about it here: https://stackoverflow.com/questions/28177917/uncaught-typeerror-cannot-read-property-createdocumentfragment-of-undefined – Josh Bradley Apr 27 '20 at 21:10
  • @JoshBradley thanks! but do you know why in jQuery docs it's this way? – Eduardo Iglesias Apr 27 '20 at 21:12
  • 1
    Actually, I was wrong. It is because you are using arrow functions, which [do not have their own binding to this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#Arrow_functions). If you use `.each(function() {console.log($(this).html()}` that will work. – Josh Bradley Apr 27 '20 at 21:17
  • @JoshBradley, Yes I change the arrow functions and it works. I didn't know about the different biding to this with arrow function. Thanks! – Eduardo Iglesias Apr 27 '20 at 21:27
  • @showdev Yes. is the same as Josh link. Thanks – Eduardo Iglesias Apr 27 '20 at 21:42

0 Answers0