Scott
Scott

Reputation: 2760

Data Attrbute - get and set in jquery

I have a number of tables on my page with the data attribute that I need to access in my document ready function, as follows, and also I show the HTML.

$('.accountsHoldingTable').DataTable({
    dom: 'tB',
    responsive: true,
    "lengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
    buttons: [
        {
            extend: 'excelHtml5',
            filename: "Positions " +  $(this).data("ttitle") + " " + today.toDateString()
        }
    ],
});
<table     id = "accountsHoldingTable" 
  data-ttitle = "my file name" 
        class = "headerSet table table-bordered display hover no-wrap dataTables accountsHoldingTable" 
        style = "width: 100%">

However, this gives me a result of

"Positions undefined Mon Dec 04 2017 (2).xlsx"

How do I do this properly so that undefined is replaced by the data-ttitle value?

Upvotes: 0

Views: 62

Answers (1)

Justin Heath
Justin Heath

Reputation: 401

I would guess that you are using this in the wrong context. See if this code accomplishes what you are are attempting to achieve:

$('.accountsHoldingTable').each(function(i, val) {
   $(this).DataTable({
     dom: 'tB',
     responsive: true,
     lengthMenu: [[25, 50, 100, -1], [25, 50, 100, "All"]],
     buttons: [
       {
          extend: 'excelHtml5',
          filename: "Positions " +  $(this).data("ttitle") + " " + today.toDateString()
       }
     ]
  });
});

Upvotes: 1

Related Questions