skip to Main Content

I want to delete a div with a dynamically created id.

i capture the id via the attr i.e

var rowToDelete = $(this).attr('data-name');

I then tried this:

 $(# " + rowToDelete + ").remove();

and this:

$(#rowToDelete").remove();

none worked. I am not concatenating the values correctly.

How do I correctly concatenate the value to the ‘#’ .

2

Answers


  1. You are messing up quotes, so there is syntax errors, check console.

    You only need to quote # character (assuming data-name contains valid ID reference):

    $('#' + rowToDelete).remove()
    
    Login or Signup to reply.
  2. You have to set # Selector as static string & rowToDelete variable as dynamic, like this :

    $("#" + rowToDelete).remove();
    

    OR

    You can use template literals, like this :

    $(`#${rowToDelete}`).remove();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search