user1464139
user1464139

Reputation:

Why can I not access the data attribute of an element with jQuery.

I have the following HTML:

    <div  class="button disabled dialogLink"
        id="edit"  
        data-action="Edit" >
        <div class="sprite-blank" ></div>
    </div>

This javascript

$('.dialogLink')
    .click(function () {
        adminDialog(this);
        return false;
    });


function adminDialog($link) {
    "use strict";
        link = {
            action: $link.data('action') || ''

I get an error saying

Uncaught TypeError: Object #<HTMLDivElement> has no method 'data'

Does anyone have an idea what I am doing wrong. It seems very simple code so I can't understand what's wrong.

Upvotes: 2

Views: 1116

Answers (1)

Ram
Ram

Reputation: 144739

You should first create a jQuery object for using jQuery object's methods like data method. You can use dataset object:

$link.dataset.action

or jQuery data method:

$($link).data('action')

Upvotes: 3

Related Questions