0

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.

2
  • 2
    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 Commented Nov 3, 2013 at 21:32
  • Use .on instead of .live, and please check the names, as you got different attribute names in js than in html. date != data. Commented Nov 3, 2013 at 21:33

2 Answers 2

2

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

Fix that and you'll be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

before going for my calculus exam in MS my professor told me, son, remember sometimes you turn blind with opened eyes, sometimes complicated questions have very simple answers...
0
  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');
    .
    .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.