Reputation: 1
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
Reputation: 177885
.live is deprecated in favour for .on
did you wrap this in $(function() {}) ?
it is data-
not date-
use window.console&&console.log
instead of alert
pass event and preventDefault:
Like this
$(function() {
$(".loadCustomerInfo").on('click',function(e) {
e.preventDefault();
var idVar=$(this).data('customerid');
.
.
Upvotes: 0
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