How to use $(this).text() as Id selector

I extracted text from table <p> tag and then I want to use it as id selector.

So I tried $('#'+$(this).text()) but it didn't work. I checked that $('#'+'string') is working but I don't know why that is not working..

this is my jquery code

$("#chosen").on("click", "tr td:not(:last-child)", function() {
    var tr = $(this).closest('tr');
    var col3 = tr.find("td:eq(3)").find('P');
    $(col3).each(function() {
        alert($(this).text());
        var data = $('#'+$(this).text()).mouseout().data('maphilight') || {};
        data.alwaysOn = !data.alwaysOn;
        $('#'+$(this).text()).data('maphilight', data).trigger('alwaysOn.maphilight');
    });
    var col4 = tr.find("td:eq(4)").find('P');

});

And this is table that I extract text:

<table class="table table-bordered table-sm table-hover">
    <thead>
        <tr class="thead-light border">
            <th>name</th>
            <th>difficulty</th>
            <th>goal</th>
            <th>recommended</th>
            <th>return</th>
        </tr>
    </thead>
    <tbody id="chosen">
        <tr>
            <td>aaa</td>
            <td>1</td>
            <td>50</td>
            <td>
                <p>recommended1</p>
                <p>recommended2</p>
                <p>recommended3</p>
            </td>
            <td>
                <p>return1</p>
            </td>
        </tr>
        <tr>
            <td>bbb</td>
            <td>2</td>
            <td>100</td>
            <td>
                <p>recommended1</p>
            </td>
            <td>
                <p>return1</p>
            </td>
        </tr>
    </tbody>
</table>

++)Sorry, I forgot to post codes that has id

<map name="image-map">
    <area id="recommended1" target="" alt="1" title="헤이븐" coords="118,392,9" shape="circle">
    <area id="recommended2" target="" alt="2" title="기계무덤 : 기계무덤 입구" coords="68,415,4" shape="circle">
                // I skipped almost for readability. 
</map>


Read more here: https://stackoverflow.com/questions/65712420/how-to-use-this-text-as-id-selector

Content Attribution

This content was originally published by Hyuenjun at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: