Jon Wisniewski
Jon Wisniewski

Reputation: 97

Object doesn't support property or method 'data'

The issue I am having, is when I am trying to update a text element through the data key, it gives me "Object doesn't support property or method 'data'" error.

I have used this before on MealNameError function to do the same thing, and it worked but when I try it on MealItemError function I am having issues.

function MealNameError(databaseMealName) {
    var $this = $("#ContentPlaceHolder1_AddMealName");
    $('#' + $this.data('describeby')).text('"' + databaseMealName + '" 
    already exists').show();
 }

function MealItemError(duplicateItems) {
    $('#addItemDropdownList_1, #addItemDropdownList_2, #addItemDropdownList_3, #addItemDropdownList_4, #addItemDropdownList_5')
    .each(function() {
        for (var i = 0; i < duplicateItems.length; i++) {
            if (parseInt($(this).val()) === duplicateItems[i]) {
                $('#' + this.data('describeby')).text('Only one of same Item').show(); //This line is throwing the error
            }
        }           
    });
 }

I would normally put data('key',value'), but the value is dynamic depending on how many food items they add. Any ideas to what am I doing different between those two problems?

Upvotes: 0

Views: 967

Answers (2)

Ele
Ele

Reputation: 33726

You have two ways to solve that problem:

  • Wrap the this context with jQuery function $
$('#' + $(this).data('describeby'))
  • Use the built-in dataset attribute.
$('#' + this.dataset.describeby)

Upvotes: 1

E. Sundin
E. Sundin

Reputation: 4181

Wrap this with $() to treat it like an jQuery element $(this).data('describeby').

In your MealNameError function your have it wrapped already.

Upvotes: 1

Related Questions