Recovering item in another function with jquery/js

Asked

Viewed 55 times

1

How can I save an item via jquery to be used later?

I have the following structure, where it retrieves the item of an onclick event from a button, retrieve the row of the table to which this button is associated and display the information relating to that line with a modal

RemoveTableRow = function(item) {   

     var Linha = $(item).closest('tr');

     var Codigo   = $(Linha).find('td[data-codigo]').data('codigo');

     AbreModal('Deseja realmente excluir esse Contato?');

    return false;         
}

then when the user clicks on the delete button belonging to modal, to remove the table row would like to recover that item to be able to perform the actual function to delete:

var tr = $(item).closest('tr'); 

tr.fadeOut(400, function() {              
    tr.remove();            
}); 
  • Instead of removing, you can just hide... and if you need the data again, it will still be on the page, although hidden.

  • but I can’t catch it because I’m sending the item in the onclick event of the delete button that has in each row of the table ... and I need to pick it up on the confirmation button which is a modal button event

1 answer

3


How you are capturing said code in function RemoveTableRow in:

var Codigo   = $(Linha).find('td[data-codigo]').data('codigo');

You can send this code in the function AbreModal as a second parameter:

AbreModal('Deseja realmente excluir esse Contato?', Codigo);

And include in the modal button the code passed in the function. Something like:

function AbreModal(mensagem, codigo){
   // exibe a modal com os parâmetros "mensagem" e "codigo"
}
  • sorry if I didn’t know how to explain it right, but I don’t want to pass the code via parameter and yes the element Line via parameter and I don’t want to pass it to the abbmodal function but when I click on a button that is inside the modal ...

  • this item 'line' will only be used if the user clicks yes because in the modal window will appear yes or no ... so the click button event will be in another function ... and so I said 'How can I save an item via jquery to be used later? ' when I speak later is with respect to performing the modal yes button click function

  • @Brunoasimilitudesilva You can use the variable Codigo created in function RemoveTableRow, but you have to take the var so that this variable can be captured in another function. Understood?

  • I got it buddy, I know how it works parameter pass, but I guess you still don’t understand kkkkk

  • @Brunoasimilitudesilva rsrs... you want the modal "YES" button to get the line code, it’s not?

  • No, I want the modal yes button to pick the var line that in the case is the <tr> that I was able to recover because of the table button

  • @Brunoaparecidodasilva then? It’s not enough to ask what has already been put on var Linha = $(item).closest('tr');? Now, I’d have to take the var for variable Linhahave a global scope.

  • Excuse my ignorance, I didn’t know I could use that as a global variable. I call two methods that have no relation at any time and needed the information of the other ... but as I can define global variable it is possible to do this, thank you very much

  • @Brunoasimilar to Silva Yes, just take the var

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.