I am new to javascript and web development, now I am experimenting in codeigniter and I want to setup a javascript confirmation box on a delete link. Now I got it to work reasonably well using this:
<script type="text/javascript">
function con(message) {
var answer = confirm(message);
if (answer)
{
return true;
}
return false;
}
</script>
and this:
echo '<a href="/groups/deletegroup/'.$group->id.'" OnClick="return con(\'Are you sure you want to delete the group?\');" class="btn small light-grey block">Delete</a>';
The problem I have is that the first time one clicks the link there is no popup, but every click after that works as it should. I also feel that maybe I should use document.getElementById and window.onload to get it to work properly. Any help would be greatly appreciated.
I eventually used a jquery solution looking like this:
$(document).ready(function (){
$('.delete').click(function (e) {
e.preventDefault();
var href = $(this).attr('href');
$.msgAlert({
title: 'Delete'
,text: 'Do you really want to delete this group?'
,type: 'error'
,callback: function () {
location.href = href;
}
});
})
});
I use the commercial msgUI for the msgAlert box.