Reputation: 2466
I'm unable to get id of the clicked element using this
as it returns 'undefined'. I am also unable to pass any parameter to the function.
What am I missing here? If I use class
name instead of pointing to the element as $(this)
, I can get the first item's id but it doesn't work if I point using $(this).
if(data && data[0].type) {
var parameter="type";
typeMsg +="Car Type : ";
typeMsg +="<table>";
$.each(data[0].type , function( index, value ){
console.log(value);
typeMsg +="<tr><td>"+ value +"</td><td class='text-center'><a href='#' id='"+value+"' onclick='deleteThis(parameter)' class='delete'><img src='../img/cross.png'></a></td></tr>";
});
typeMsg +="</table>";
}
function deleteThis(param) {
console.log(param); //outputs nothing
var deleteId = $(this).attr("id");
console.log(deleteId); //says undefined
}
Upvotes: 0
Views: 78
Reputation: 15913
Instead of parameter send this
with onclick
, It will get you the current element and you can get the id of the same then
function deleteThis(param,text) {
alert(text);
var deleteId = $(param).attr("id");
alert(deleteId)
console.log(deleteId);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href='#' id='qwe' onclick="deleteThis(this,'tushar')">click</a>
Upvotes: 2