I am working on jquery with php,Right now i have button inside loop and i want to pass "data attribute" and want to get value,Right now i am getting "undefined",So how can i do this ?
Here is my current code
echo "<td>"."<a class='map-pop' data-id='".$employee_time['id']."' data-toggle='modal' data-target='#mapModal' onclick='viewmap();'><img src='assets/viewmap.png'></a>"."</td>";
<script>
function viewmap()
{
var ids = $(this).attr('data-id');
alert('id is ' + ids);
}
</script>
4
Answers
change your onclick to this
and get data with jquery like this
Pass this in onclick function and get in viewmap function and access
You can try below code ..
PHP
Script
The problem is that you are using
this
inside your functionviewmap
but you are not sending it as a parameter from theHTML
code, Change youronclick
by:onclick='viewmap(this);
than change thejs
function like bellow:Remove
onclick
attribute and change theviemap()
function like this`