Miguel Mas
Miguel Mas

Reputation: 545

Jquery: not removing rows from html table

I have a html table and I need to remove some rows according to the ids passed by a function.

Here's my code:

<html>
    <script>
    function myfunc(){
        var dat ="6|21|22|"; // this string is a dynamic string coming 
                             // from another function. I've hardcoded it 
                             // for clarity
        var aryDat0 = dat.split('|');
        //delete empty elements
        var aryDat = aryDat0.filter(function(v){return !!v}); 
        for (var i = 0, l = aryDat.length; i <l; i++) {
          $( "'#" + aryDat[i] + "'" ).remove();
        }
    }
    </script>
    <table name="mytab" border="1px">
        <tr id="6"> 
            <td>6</td>
            <td>ada</td>
        </tr>
        <tr id="21">
            <td>21</td>
            <td>eda</td>
        </tr>
        <tr id="22">
            <td>22</td>
            <td>ida</td>
        </tr>
    </table>
    <input type="button" value="test" 
           onclick="javascript:myfunc(); return false;">
</html>

When I press the "test" button, nothing happens and I get in Chrome's Console:

Uncaught Error: Syntax error, unrecognized expression: '#6' 

Why?? If instead of:

$( "'#" + aryDat[i] + "'" ).remove();

I do:

$('#21').remove();

It works great.

Upvotes: 0

Views: 72

Answers (2)

Balachandran
Balachandran

Reputation: 9637

Just concatenate id value to #

 $( '#' + aryDat[i]  ).remove();

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

This Error Uncaught Error: Syntax error, unrecognized expression: '#6' clearly states that $("'#6'") is an invalid Jquery selector

Try,

$( "#" + aryDat[i]).remove();

Upvotes: 2

Related Questions