简体   繁体   中英

How to select element in jQuery by function id parameter?

I want to delete an Item in my to do list App

deletItem: function(id) {
        $(id).remove();
        console.log(id);
    },

Here is the function who calls deletItem

function delItem(event) {
    var itemID, splitID, type, ID;
    itemID = event.target.parentNode.parentNode.parentNode.parentNode.id;
    splitID = itemID.split("-");
    type = splitID[0];
    ID = parseInt(splitID[1]);

    budgetCtrl.delItem(type, ID);
    UICtrl.deletItem(itemID);
    updatBudget();
    console.log(itemID);
}

But, the Item is calculating bt not getting deleted from UI only. Please help.

Try this:

deletItem: function(id) {
    $('#' + id).remove();
    console.log(id);
},

What you tried earlier won't work because when using jQuery, you must have a '#' or '.' in front of the class or id. For example, lets say the id was "deleteThis" Then, you would be basically doing:

$('deleteThis').remove();

You should be doing

$('#deleteThis').remmove();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM