Passing parameters in jquery .on

0

Hello everyone. I'm trying to do a function to delete a node from an xml, however I have a problem which is as follows: In pure javascript I just need to

  

onclick="function ('')"

Inside the html tag for it to pass the parameter coming as echo from php. But I'm having trouble doing something similar in jquery. I just replaced php with

  

$ (this) .attr ("id")

Why is it inside a each. Can you help me? Note: I already tested creating the function inside the jquery ready and it did not work.

Edit:

The xml I'm using is this

<dados><usuarios><usuario id="1"><login>marcos</login><senha>15</senha></usuario></usuarios><gastos><gasto user="1" id="1"><descricao>bk</descricao><valor>20</valor></gasto><gasto user="1" id="2"><descricao>mcdonalds</descricao><valor>15</valor></gasto></gastos></dados>

I need to display the expenses in a table, hence my jquery looks like this:

$(xml).find("gasto").each(function() {
    if($(this).attr("user") == user) {
        $("#tablex tr:last").after("<tr><td>"+$(this).find("descricao").text()+"</td><td>"+$(this).find("valor").text()+'</td><td><button onclick="apagarGasto('+$(this).attr("id")+')">X</button></td><td><button class="up">U</button></td></tr>');
    }
});

Inside the onclick you can see that I'm trying to pass the id id attribute. However, if I put the deleteGasto function inside the $(documento).ready(function) it shows me in the console that the function is undefined, that is, that it does not exist. So, I thought the normal onclick of pure javascript does not work when using jquery, am I right? So, I need a way that can pass the id id parameter to a function inside the jquery. Please if I have not been clear enough, tell me! Thanks in advance!

    
asked by anonymous 08.06.2017 / 08:24

1 answer

0

You can declare your listener with on and get a date attribute with its parameter, for example:

I changed the button a bit to pass the date attribute and add the class:

$(xml).find("gasto").each(function() {
    if($(this).attr("user") == user) {
        $("#tablex tr:last").after("<tr><td>"+$(this).find("descricao").text()+"</td><td>"+$(this).find("valor").text()+'</td><td><button class='btn-click' data-id=' + $(this).attr("id")+'">X</button></td><td><button class="up">U</button></td></tr>');
    }
});

and your listener for the click event on the button:

$(document).on("click", ".btn-click", function(){
    var id = $(this).data("id");
});

I just did not understand that there was an indefinite function return in the example you mentioned.

    
08.06.2017 / 19:28