0

HTML

<input type="number" id="progress-update-1234" class="progress-update" data-entryid="1234" data-updateItem="progress" value="3" min="1" max="15" />

jQuery

$( ".progress-update" ).change(function() {

    var entry_id = this.$.attr('data-entryid');
    var item_entry = this.$.attr('data-updateItem');
    var new_progress = $("#progress-update-" + entry_id).val();

    alert( "This is progress update" + entry_id + " and type " + item_entry + " and new progres " + new_progress );
});

I'm not exactly sure why this is returning an error: Cannot call method 'attr' of undefined in the console. I am using this inside the change function that is attached to the right class. Why am I getting this error?

Maaz
  • 4,193
  • 6
  • 32
  • 50

3 Answers3

1

Use it like this:

var entry_id = $(this).attr('data-entryid');

attr is a jQuery function that's why it can use only with jQuery objects. this not a jQuery object but $(this) is.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

your code should look like this

var entry_id = $(this).attr('data-entryid');
Damien Black
  • 5,579
  • 18
  • 24
1

You must wrap the element with jQuery

var entry_id = $(this).attr('data-entryid');
var item_entry = $(this).attr('data-updateItem');
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189