4

JS/JQuery newbie here. I'm using BootBox for Twitter Bootstrap, and I'm trying to pass the value of the id attribute of an a element into the callback function:

<a class="confirmdel" id="<?= $folioid ?>" href="#"> Delete</a>

<script>
$(document).on("click", ".confirmdel", function(e) {
    var folioid = ($(this).attr('id'));
    bootbox.dialog("Do you really want to delete this folio entry?", [{
        "label" : "Yes",
        "class" : "btn-danger",
        "icon"  : "icon-trash",
        "callback": function() {
            window.location.replace("deletefolio.php?folioid="+$folioid);
        }
    }, {
        "label" : "No",
        "class" : "btn",
    }]);
});
</script>

It seems i can't use $(this) inside the callback function because in there it doesn't refer to the a element but to the window instead, and I have no idea how to pass the variable into the callback from outside.

Maybe I'm thinking about it the wrong way entirely.

Any suggestions greatly appreciated!

stoo
  • 97
  • 1
  • 5
  • 5
    you got a typo at `window.location.replace("deletefolio.php?folioid="+$folioid);` try change `$folioid` into `folioid ` – Michael Yin May 10 '13 at 12:01
  • 1
    **/facepalm/** Thanks. It works now :P – stoo May 10 '13 at 12:44
  • Note that you always need to set folioid before each call to bootbox.dialog() in this way (e.g. in the clickhandler like above) and can't use a global-scoped variable or you will run into issues where multiple instances of the callback will use the same parameter because it will be read on callback execution. (Hoping to have made the point clear). – Manuel Arwed Schmidt Sep 26 '15 at 15:37

0 Answers0