I have done a datatable which will get data from web service and fill the data itself. I have introduced an action button on a column in the datatable, which has <i class="icon-lock">
. Now when i click this icon, it should toggle and changed into <i class="icon-unlock">
$('#test').dataTable({
"aaData": realdata,
"aoColumns": [// "bVisible": false (To disable
{ "sTitle": "First Name", "mData": "FirstName", "sClass": "header2" },
{ "sTitle": "Last Name", "mData": "LastName", "sClass": "header2" },
{ "sTitle": "Email ID", "mData": "Email", "sClass": "header2" },
{ "sTitle": "Role", "mData": "Role", "sClass": "header2" },
{
"sTitle": "Action", "fnRender": function (oObj) {
if (oObj.aData["IsActive"] == "Y")
ActionHtml += "<a href='#' class='bs-tooltip Enable' title='' data-original-title='Search'><i class='icon-lock' onclick='EnableUser();'/></i></a> | ";
else
ActionHtml += "<a href='#' class='bs-tooltip Enable' title='' data-original-title='Search'><i class='icon-unlock' onclick='EnableUser();'/></i></a> | ";
return ActionHtml + '</span>';
}
}
]
});
});
function EnableUser() {
$('.icon-lock').toggleClass('icon-unlock');
}
Now when click the icon, i can get the toggle functionality with icon changing from icon-lock to icon-unlock .. But it is applied to all icon in all the rows in that column. I want it to apply only one row at a time.
try this
$(document).ready(function(){
$('.icon-lock, .icon-unlock').on('click',function(){
$(this).toggleClass('icon-lock').toggleClass('icon-unlock');
});
});
see demo DEMO
Remove the onclick from the HTML and bind the click in JS like this
$(document).on('click','.Enable', function(){
$(this).find('.glyphicon').toggleClass('glyphicon-lock glyphicon-unlock'); // using glyphicon as class here for the demo
})
Your code wasn't working because you are not passing this
to onclick=EnableUser()
. You can then use this
and toggle the classes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.