Talki
Talki

Reputation: 33

jquery selector with specific class and data attribute

I have a litte problem with selecting a table row with specific class and a specific data attribute.

I hope anyone has an idea.

This is the HTML source

    <table id="source" class="connectedSource" width="100%">
    <thead>
    ...
    <tbody>
    ...
    <tr class="headrow" bgcolor="#c2c2c2">
    ...
    <tr data-obj_id="904" class="datarow ui-draggable" bgcolor="#ffffff">
    ...
    <tr data-obj_id="906" class="datarow ui-draggable" bgcolor="#ffffff">
    ...
    <tr data-obj_id="908" class="datarow ui-draggable" bgcolor="#ffffff">
    ...
    <tr data-obj_id="903" class="datarow ui-draggable" bgcolor="#efefef">
    ...

This works fine ...

    if ($.browser.mozilla) console.log('selector = ' + "tbody tr.datarow");
    if ($.browser.mozilla) console.log('source row class = ' + $("#source").find("tbody tr.datarow").attr('class'));
    if ($.browser.mozilla) console.log('source row obj_id = ' + $("#source").find("tbody tr.datarow").data('obj_id'));

The result ...

    selector = tbody tr.datarow
    source row class = datarow ui-draggable
    source row obj_id = 904

This doesn't work ...

    if ($.browser.mozilla) console.log('selector = ' + "tbody tr.datarow[data-obj_id='" + source_obj_id + "']");
    if ($.browser.mozilla) console.log('source row class = ' + $("#source").find("tbody tr.datarow[data-obj_id='" + source_obj_id + "']").attr('class'));
    if ($.browser.mozilla) console.log('source row obj_id = ' + $("#source").find("tbody tr.datarow[data-obj_id='" + source_obj_id + "']").data('obj_id'));

The result ...

    selector = tbody tr.datarow[data-obj_id='875']
    source row class = undefined
    source row obj_id = undefined

I have to select the datarow with data-obj_id = 908

I hope anyone can help ...

Thanx

Talki

Upvotes: 1

Views: 4570

Answers (1)

bipen
bipen

Reputation: 36541

since your obj_id is unique.. i don't think you need class selector there

try with

 "tbody tr[data-obj_id='" + source_obj_id + "']"

Upvotes: 3

Related Questions