user2950777
user2950777

Reputation: 1

jquery data element returning undefined

I have two links in my html code. I use both of them to read the data element using jQuery. However it works fine for one of them and for the other it returns undefined values.

the html is as follows

<a class="loadCustomerInfo" href="#Augustinho" date-customerid="1" date-customername="Augustinho" date-customeraddress="mercado shoprite" date-companyname="Auto Kilimanjaro" date-phoneone="828759460" date-city="0">Augustinho</a>

the following jQuery I use

$(".loadCustomerInfo").live('click',function(){
  //alert("add item");
  var idVar=$(this).data('customerid');
  // alert($(this).innerHTML);

  alert($(this).attr('data-customername'));

  alert($(this).data('customername'));
  var randid = new Date().getUTCMilliseconds();

  $("#customerInfoTable").append('<tr><th id="customerInfoTH" colspan=2>Customer Information</th></tr>' +
  '<tr><th>Customer Name</th> <td>' +$(this).data('customername')+ '</td></tr>' +
  '<tr><th>Company Name</th><td>' +$(this).data('companyname')+ '</td></tr>' +
  '<tr><th>Address</th><td>' +$(this).data('customeraddress')+ '</td></tr>' +
  '<tr><th>City</th><td>' +$(this).data('city')+ '</td></tr>' +
  '<tr><th>Primary Phone</th><td>' +$(this).data('phoneone')+ '</td></tr>');


  //return false;
});

The click function is working as I get the response from alert() but always undefined, also in my html it displays undefined for all variables.

Upvotes: 0

Views: 195

Answers (2)

mplungjan
mplungjan

Reputation: 177885

  1. .live is deprecated in favour for .on

  2. did you wrap this in $(function() {}) ?

  3. it is data- not date-

  4. use window.console&&console.log instead of alert

  5. pass event and preventDefault:

Like this

$(function() {
  $(".loadCustomerInfo").on('click',function(e) {
    e.preventDefault();
    var idVar=$(this).data('customerid');
    .
    .

Upvotes: 0

Sirko
Sirko

Reputation: 74046

Your attributes in the HTML code are all starting with date- instead of data-.

Fix that and you'll be fine.

Upvotes: 2

Related Questions