Во-первых, обратите внимание, что вы включаете jQuery несколько раз. Одна из ссылок может быть удалена.
Что касается проблемы, то она связана со значением в аргументе, который вы предоставляете функции, требующей кавычек вокруг нее. Правильный синтаксис был бы:
row += '<td><button onclick="JavaScript:showDetails(\"' + area + '\");">' + 'View Details' + '</button></td>';
Однако гораздо лучший подход - избегать использования встроенных обработчиков событий, поскольку они являются плохой практикой из-за устаревания и отсутствия разделения интересов принцип. Вместо этого используйте делегированные обработчики событий. Как вы уже включили jQuery на странице, это будет выглядеть так:
// mock AJAX response data:
let childNode = [{
"name": "shruthy",
"age": "31",
"country": "india",
"state": "tamilnadu",
"city": "chennai",
"cityDetails": {
"area": "mylapore",
"station": "kilpauk"
}
}, {
"name": "arsha",
"age": "31",
"country": "india",
"state": "tamilnadu",
"city": "bengaluru",
"cityDetails": {
"area": "koramangala",
"station": "cantonment"
}
}]
// inside success(childNode)
let html = childNode.map(n => {
let row = '<tr>'
row += '<td>' + n.name + '</td>'
row += '<td>' + n.age + '</td>'
row += '<td>' + n.country + '</td>'
row += '<td>' + n.state + '</td>'
row += '<td>' + n.city + '</td>'
row += '<td><button class="showdata" data-area="' + n.cityDetails.area + '">View Details</button></td>'
row += '</tr>';
return row;
});
$('#myTable').append(html);
// somewhere outside the AJAX call:
$('#myTable').on('click', '.showdata', function() {
console.log($(this).data('area'));
});
table {
width: 50%;
}
th {
background: #f1f1f1;
font-weight: bold;
padding: 6px;
}
td {
background: #f9f9f9;
padding: 6px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="myTable">
<tr>
<th>name</th>
<th>age</th>
<th>country</th>
<th>state</th>
<th>city</th>
<th>action</th>
</tr>
</table>
Вот полный пример, показывающий всю структуру HTML:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8">
<title>Insert title here</title>
<style>
/* NOTE: This should be in an external .css file */
table {
width: 50%;
}
th {
background: #f1f1f1;
font-weight: bold;
padding: 6px;
}
td {
background: #f9f9f9;
padding: 6px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="application/javascript">
jQuery($ => {
$('#myTable').on('click', '.showdata', function() {
console.log($(this).data('area'));
});
$.ajax({
url: 'childNode.json',
type: 'GET',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(childNode) {
let html = childNode.map(n => {
let row = '<tr>'
row += '<td>' + n.name + '</td>'
row += '<td>' + n.age + '</td>'
row += '<td>' + n.country + '</td>'
row += '<td>' + n.state + '</td>'
row += '<td>' + n.city + '</td>'
row += '<td><button class="showdata" data-area="' + n.cityDetails.area + '">View Details</button></td>'
row += '</tr>';
return row;
});
$('#myTable').append(html);
},
error: function(jqXHR, textStatus, errorThrown) {
// alert('Error: ' + textStatus + ' - ' + errorThrown);
}
});
});
</script>
</head>
<body>
<table id="myTable">
<tr>
<th>name</th>
<th>age</th>
<th>country</th>
<th>state</th>
<th>city</th>
<th>action</th>
</tr>
</table>
</body>
</html>