Search code examples
jquerybootbox

How to get object inside another object in JQuery


How can i get the li that i clicked inside bootbox.confirm()?

<ul id="something">
  <li>Result 1</li>
  <li>Result 2</li>
  <li>Result 3</li>
</ul>

$('#something li').click(function(){
    bootbox.confirm("Are you sure?", function(result) {
    if(result == true){
        alert("You deleted" + /*here I want to get the item that was clicked*/);
        }
    }); 
});

Solution

  • cache the context with this:

    $('#something li').click(function(){
        var $this = this; // cache it here
        bootbox.confirm("Are you sure?", function(result) {
            if(result == true){
               alert("You deleted" + $this.textContent/*use it here now!!*/);
            }
        }); 
    });