Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery remove table row with non-standard id characters

I am trying to remove a table row using jQuery like this

function removeTableRow(trId){
    $('#' + trId).remove();
}

However this doesn't work if the id contains a character like "%", "^", "&", ",", etc....

Do you know if there is any work around for this?

like image 822
Timothy Ruhle Avatar asked Dec 07 '10 23:12

Timothy Ruhle


4 Answers

I believe the reason why can be found here: What are valid values for the id attribute in HTML?

However I'm not so sure about a workaround other than the obvious (change your ids)

like image 140
Chris Simpson Avatar answered Oct 29 '22 23:10

Chris Simpson


HTML 4.0 IDs cannot contain these characters and be valid at the same time:

Attribute values of type ID and NAME must begin with a letter in the range A-Z or a-z and may be followed by letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). These values are case-sensitive.

If you must, you can try this:

function removeTableRow(trId) {
    $(document.getElementById(trId)).remove();
}

I'd recommend fixing the HTML, though.

like image 32
Tomalak Avatar answered Oct 29 '22 21:10

Tomalak


I wouldn't suggest using those characters in an id string. However if you feel it necessary then you need to use \\ to escape the character in the selector.

Example: http://jsfiddle.net/NuWSp/

<table>
    <tr id="b%b">
        <td>hello</td>
    </tr>
    <tr>
        <td>world</td>
    </tr>
</table>


function removeTableRow(trId){
    $('#' + trId).remove();
}

removeTableRow( "b\\%b" );
like image 8
ghindle Avatar answered Oct 29 '22 22:10

ghindle


It's better if it is several rows in many tables to remove them through another attribute such as class or group.

Here is an example of how to remove via group attribute:

$("table tr[group='"+groupname+"']").remove();

I hope this helps.

like image 4
av_master Avatar answered Oct 29 '22 22:10

av_master