Pergunta

I have a button <button onclick="takedown()"> that when it is clicked JQuery will create a new button with the id of the text in a input and button, when that button is clicked it will call a function deltebutton() which should delete the button, but I need for JQuery to get the id of the button which called the function and set it to a String called id.

I tryed $(event.target).attr('id'); and $(this).attr('id'); but both of them did not work I goggled it but could not find anything.

This is the functions

    function takedown(){

note = document.getElementById("noteinput").value;

idh1 = note + "h1";
idbutton = note + "button";
idcenter = note + "center";

$('<center id="' +idcenter + '"> <h1 idh1="' + idh1 + '">' + note + '</h1> <button id="'+ idbutton +'" onclick="deletenote()"> Delete </button> </center>').appendTo("body");


}

and

function deletenote(){

    String id = $(event.target).attr('id');

    $(id).remove();

}

If anybody knows how to do this, it will be very helpful.

Foi útil?

Solução

Dont use String in JS and jQuery. And your selector is wrong.

function deletenote(){
    var id = event.target.id;
    $('#'+id).remove();
}

Also I think you can just call remove without getting the ID.

function deletenote(){
    $(this).remove();
}

Outras dicas

A simpler solution building on the previous answer and comments would be:

function deletenote(){
   $(event.target).remove();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top