Syl
Syl

Reputation: 1174

Select a table row base on the content of a custom attribute

So I have a table with multiple rows. the first TD of each row content a custom attribute tag called lineup. Ex:

<td lineup="['463', '442', '200', '97', '238', '548', '166', '184', '353', '26', '573']">Some stuff</td> 

This is actually a list I pass into the html through Jinja2. I figured it was the best solution to store this data. Maybe I'm wrong, I'm just starting to code.

Now what I want to do is select the row when the lineup contain a specific ID.

Right now I'm doing it like so:

$("#xtable tbody td[lineup*='"+ID+"'']").parent()

Which work great except if the ID is 63, it will also select the row with the id 463. I understand why it is doing this since it's basically searching a string, he doesn't know that 463 is actually not 63.

So my questions:

EDIT:

This is my Jquery function:

$('#update li').hover(function(){
        playerId = $(this).find('span').attr("pid");
        $("#xtable tbody td[lineup*='"+playerId+"'']").parent().css("background-color","#d9edf7")
        },
        function(){
            $("#xtable tbody td[lineup*='"+playerId+"'']").parent().css("background-color","")
});

Example of a li element to get the playedId

<li class="pushmsg" style=""><p><span rel="tooltip" class="player-name" pid="14" data-original-title="total point: 3">Arshavin</span> just got an assist</p></li>

Thanks!

Upvotes: 0

Views: 2835

Answers (3)

arnoud
arnoud

Reputation: 147

The best would be to include the apostrophes in the string that you filter for. You should prepend it with a backslash, as you want it to be seen as a string.

$("#xtable tbody td[lineup*='\'"+ID+"\'']").parent()

I would recommend you against custom attributes as much as possible, but if you do need them, use a data-* attribute. More information here and here. With data-lineup, your code would look like this:

$("#xtable tbody td[data-lineup*='\'"+ID+"\'']").parent()

Upvotes: 2

seutje
seutje

Reputation: 711

$('#xtable tbody td[lineup*="\''+ID+'\'"]').parent()

Included the single quotes around your IDs in the attribute substring to match, but escaped.

Also: you win the award for awkwardness.

Example fiddle: http://jsfiddle.net/GHmjr/

Upvotes: 1

Om3ga
Om3ga

Reputation: 32951

Remove asterisk *

 $("#xtable tbody td[lineup='"+ID.toString()+"'']").parent()

Upvotes: 1

Related Questions