3

I had a problem and I am struggling. I want to redirect to the link href="/jobs/emp/del/?id={{ job.pk }}" after confirmation. I am using bootboxjs

{% for job in jobs %}
<a class="btn deleteJob" href="/jobs/emp/del/?id={{ job.pk }}"</a> 
{% for job in jobs %}

How can I require confirmation before executing the link /jobs/emp/del/?id={{ job.pk }}?

I tried:

<a class="btn deleteJob"><span style="display:none">/jobs/emp/del/?id={{ job.pk }}</span>
                            <i class="icon-trash"></i></a>
<script src="/static/js/bootbox.js" type="text/javascript"></script>
<script>

$('.deleteJob').click(function(){
    bootbox.confirm("Are you Sure want to delete!", function (x) {
    if (x){

        var link= $(this).find( $spans ).text();            
        document.location.href='/'+link+'/';}
    });        
});
</script>

is inside a big for loop, but it is not working!?

jhoyla
  • 1,191
  • 1
  • 9
  • 26
suhailvs
  • 20,182
  • 14
  • 100
  • 98

5 Answers5

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.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
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

Novocaine
  • 4,692
  • 4
  • 44
  • 66
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.

Bobby5193
  • 1,585
  • 1
  • 13
  • 23
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.

Community
  • 1
  • 1
XapaJIaMnu
  • 1,408
  • 3
  • 12
  • 28
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();
        }
    });
});
Mahdi jokar
  • 1,267
  • 6
  • 30
  • 50