0

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.

1
  • First way to debug code is to write clean one! I edited your post code for better readability! Commented Jun 4, 2014 at 12:58

2 Answers 2

2

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

Try,

$( "#" + aryDat[i]).remove();
Sign up to request clarification or add additional context in comments.

3 Comments

The + "" is certainly very useful.
hahaha, great! thanks a ton! I'llbe acepting your answer in a moment. Thanks again!
@dystroy MR.OP, please don't note that.. :D
0

Just concatenate id value to #

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

Comments

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.