Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting a dynamically generated string

On button click, I want to send data to the server for a certain line. Rows in the table are created dynamically

There is the following code:

<tr class="ordertr table-bordered">
<td id="recid" class="d-none"><?=$row['RecId']?></td>
<td class="ps-lg-3">
<ul>
<li><button id="editingST">&#9998;</button></li>
<li><button class="towork">đź› </button></li>
</ul>
</td>
</tr>

And JS:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$("#position-order tbody").on('click', '.towork', function gotowork(){
 $.ajax({
type: "POST",
url: '/towork.php',
data: {recid: $('#recid').text()}
});
return false;
});

recid is defined, but for all rows it takes the value from the first row

I think there is an error here – $("#position-order tbody")

>Solution :

You can get the row containing the button, then query for that specific td.

function gotowork(e) {
    const recid = $(e.target).closest('tr').find('#recid').text();
    // make request...
}

However, ids should be unique in a document, so you should use a class for this instead.

<td class="recid d-none">text...</td>
$(e.target).closest('tr').find('.recid').text()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading