0

Problem: I am trying to replace elements of a certain class with an empty string.

What i've tried

<table>
    <tr>
         <td>Cell to keep</td>
    </tr>
    <tr class='hide-report-item'>
        <td>Remove Me</td>
     <tr>
</table>

<script>
<!-- In my scenario data is actually a partial coming back from an ajax call) -->
function ParseData(data)
{
                var foo = $(data)[2];
                var hideItems = $(foo).find('.hide-report-item');

                hideItems.each(function () {
                    var test = $(this)[0];
                    data = data.replace(test.outerHTML, '');
                });
}
<!-- Ive also tried different variations of this -->
function ParseData(data)
{
                var foo = $(data).find('.hide-report-item').hide();
                var hideItems = $(foo).html();
}

<script>

Expected output:

<table>
    <tr>
         <td>Cell to keep</td>
    </tr>
</table>

Any help would be appreciated. thanks

1

2 Answers 2

1

Add a class to the elements you want to remove, then .remove() them with jQuery

$(function() {
 $( ".hide-report-item" ).remove();
});
Sign up to request clarification or add additional context in comments.

4 Comments

data comes in as a string. When i convert it using for example foo = $(data) foo is an array [0] <br /> [1] text [2] tablerow. Any kind of remove or hide and foo.html() returns an empty string
But that data still just ends up in the DOM right? Why can't it be manipulated with jQuery after load?
oh i see what youre saying. gimme a second to try it
No problem at all :)
1

You can simply use the remove() method.

$('.classToRemove').remove();

No need to use each() or any other loop. This will find all of those elements with that class in the DOM.

1 Comment

data comes in as a string. When i convert it using for example foo = $(data) foo is an array [0] <br /> [1] text [2] tablerow any kind of remove or hide and foo.html() returns an empty string

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.