Pass parameters in jquery . on

Asked

Viewed 501 times

0

Hey there, guys. I’m trying to do a function to delete a node from an xml, but I have a problem that is the following: In pure javascript just by me

onclick="function('')"

inside the html tag for him to pass the parameter coming as php echo. However, I’m having trouble doing something similar in jquery. I just replaced php with

$(this). attr("id")

Why are you inside a each. Can you help me? Note: I already tested creating the function inside the jquery ready and it didn’t 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, then 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 am trying to pass the expense id attribute. However, if I put the erase functionGost inside the $(documento).ready(function) it shows me on the console that the function is undefined, ie that it does not exist. So I thought that the normal pure javascript onclick doesn’t work when using jquery, am I right? So, I need a way that I can pass the expense id parameter to a function within jquery. Please if I haven’t been clear enough, tell me! Thank you in advance!

  • 1

    Do you want to remove the clicked node, is that it? Add the xml related to the problem to make it clearer.

  • 2

    In addition to what Sergio asked for, if you can please put the js code that you’re running in one piece, it’s easier for us to help. (=

  • No. js default listeners work in any condition. Their function: deleteGasto, must be inside the script tags and outside of $(Document). ready.

1 answer

0

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

I changed your button a little 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 button click event:

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

I just didn’t understand that there was an undefined function return in the example you quoted.

Browser other questions tagged

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