Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: remove element by id

I have a problem that I'm trying to solve, the current way of doing it requires that I remove elements.

<div id="toremove">
    // some JavaScript here
</div>

So I have a function that does something nice, then when that finishes I'd like it to remove the "toremove" element. I have looked at the jQuery documentation, similar posts allover the Internet but nothing seems to work.

Here's what I'm doing and have tried:

$("#toremove") = $()
$("#toremove").empty();
$("#toremove").remove();

Nothing seems to work for me, any help is appreciated.

like image 379
marko Avatar asked Aug 18 '14 21:08

marko


People also ask

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.

How do you destroy an element in HTML?

HTML DOM Element remove() The remove() method removes an element (or node) from the document.


2 Answers

$('#toremove').remove(); should do what you need. Are you sure there isn't a problem elsewhere in your code?

Example

like image 166
Anthony Martin Avatar answered Oct 16 '22 13:10

Anthony Martin


You can do other of the way to deal with this issue, I will show you I know as following.( please pick anyone up you like)

<html>
<body>
    <div id="toremove">
        ...some javascript here....
    </div>
</body>
</html>
<script src="jquery-3.3.1.min.js"></script>
<script>
    $('#toremove').html('');  // option 1
    $('#toremove').attr('style', 'display:none')  // option 2
    $('#toremove').attr('style', 'visibility:hidden') // option 3
    $('#toremove').remove();  // option 4
    $('#toremove').hide();   // option 5
</script>
like image 36
Willie Cheng Avatar answered Oct 16 '22 15:10

Willie Cheng