0

I have a variable x which counts the number of fields added to a form dynamically. I want to remove a field for which I tried to use the following code. But I can't.

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $( "div" ).remove( ".org-details\'+(x-1)+\'" );x--;
    })
});

I want the output be something like this on runtime.

$( "div" ).remove( ".org-details1" );

Please help.

4
  • 1
    $("div").remove(`.org-details${--x}`) Commented Feb 27, 2020 at 13:52
  • Thank you too. This too works. Commented Feb 27, 2020 at 13:59
  • FYI. if you use this you don't need to use x--; after remove as --x decrements and returns the value after decrementing. Commented Feb 27, 2020 at 14:02
  • Thanks again. I fixed that. Commented Feb 27, 2020 at 14:12

1 Answer 1

1

You should use template literals (note the backticks):

$("div").remove(`.org-details${x-1}`)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Exactly what I wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.