S.Kav
S.Kav

Reputation: 65

set element attribute with jquery within a loop

I have a div element inside while (mysql query). So for every entry I want id to increment by 1. If I have "div id="0"" initially, with every new object I want id++. I have no idea how to do it and tried something like this:

<?php //some code
for ($i = 0; $i < $sent+1; $i++)  {
        ?><script>jQuery(".list-row").attr("id", "$i")</script> <?php //some code

.list row is a class of div element. Any ideas how to do it in right way?

Upvotes: 1

Views: 24

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

This would do it for you

<?php //some code
for ($i = 0; $i < $sent+1; $i++)  {
    echo '<script>jQuery(".list-row").attr("id", "' . $i . '")</script>';
}

Or maybe this would be better and more readable later

<?php //some code
for ($i = 0; $i < $sent+1; $i++)  {
    echo '<script>jQuery(".list-row").attr("id", "sent_' . $i . '")</script>';
}

Upvotes: 1

Related Questions