简体   繁体   中英

Grab Data ID Value

I'm trying to get the value of data-id in this a tag to be stored in the var filterValue

<a class="link-item" href="#page" data-id="1">Some Page</a>

By using the script below as I need the value of the data-id ID to be used by the Handlebars Helper

The Javascript is

    var filterValue = $("body").on('click', 'a.link-item', function (e) {
    e.preventDefault();
    $(this).data("id");

    alert("Data ID: " + $(this).data("id"));

    console.log(filterValue);

    Handlebars.registerHelper('filter', function (fValue, options) {
        if (fValue == filterValue) {
            return options.fn(this);
        } else {
            return options.inverse(this);
        }
    });

});

The alert is returning the value of data-id but somehow its not being stored in the var filterValue

I've created a JS bin here http://jsbin.com/fesejo/1/edit?html,js,output

Thanks & Regards

you are doing it wrong, you have to change it to this:

 var filterValue;
   $("body").on('click', 'a.link-item', function (e) {
    e.preventDefault();
    filterValue =$(this).data("id");//<-- store data-id value in variable
    ....................
    ....................
});

or:

   $("body").on('click', 'a.link-item', function (e) {
    e.preventDefault();
    var filterValue =$(this).data("id");//<-- store data-id value in variable
    ....................
    ....................
});

UPDATED JS BIN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM