stack stack - 1 month ago 9
HTML Question

How can I get the href value of clicked element?

Here is my code:

<a onclick="return bootbox.confirm('are you sure?', function(e){ if (e) {window.location = this.href;} });" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">


Noted that I use this library for
confirm
.

But this line doesn't work in my code:

window.location = this.href;


And it always (when I click OK) redirects me here:

http://localhost:8000/undefined


How can I fix it?

Answer
<a onclick="return bootbox.confirm('are you sure?', function(e){ if (e) {window.location.href = $(this).attr('href');} });" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">

Instead of onClick you can do something like this.

<a id="some-id" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">
$('#some-id').click(function(){
    var link = $(this).attr('href');
    bootbox.confirm('are you sure ?', function(e){
      if(e){
         window.location.href = link;
       }
    })
})
Comments