[Django]-I want to redirect to link on <a> only after confimation?

7👍

I’ve not used bootbox, but assuming the x value in the function is the boolean result of the popup, this should work:

<a href="/jobs/emp/del/?id={{ job.pk }}" class="btn deleteJob">
    <i class="icon-trash"></i>
</a>
$('.deleteJob').click(function(e) {
    e.preventDefault();
    var $link = $(this);
    bootbox.confirm("Are you Sure want to delete!", function (confirmation) {
        confirmation && document.location.assign($link.attr('href'));
    });        
});

Note, you mention a for loop – the jQuery code there does not need to be in a loop. That code will work for all instances of the .deleteJob anchor element.

3👍

You can use preventDefault() to stop the redirect;

$('.deleteJob').click(function(e) {
    e.preventDefault();
    if(confirm("Are you Sure want to delete!")) {
        var link= $(this).find( $spans ).text();            
        document.location.href='/'+link+'/';}
    }
});

I’m not sure about bootbox so this is a jQuery only solution

0👍

<script>

  $('.deleteJob')each(function(){
   var href = $(this).attr("href");
   $(this).click(function(){
   if(confirm("Are you sure you want to delete this job ?"))
   {
       document.location.href='/'+href+'/';}
   }

   });

  });
</script>

maybe this could work if you don’t mind using the normal confirm window from the browser.

0👍

Is it working if you are using it outside of a loop? The problem looks like you are running into javascript closures problem, which basically means that functions assigned in loops will be overwritten the next time the loop is executed and thus the function onclick would be binded only on the last DOM that was parsed to it in the loop. In order to work around that move the onclick assignment to an external function that is called on every iteration of the loop (you probably need to parse.

Also is this valid jquery identifier?: $spans

Read about closures here.

0👍

You must prevent the default action on a if result of confirm function is false

$(document).ready(function () {
    $(".deleteJob").on("click", function (e) {
        if (!confirm("Are you Sure want to delete!")) {
            e.preventDefault();
        }
    });
});

Leave a comment