0

I'm building a simple table from an array with an edit button for each table row. The value passed to the php page on submit via POST needs to be associated with the qnumber for the line. Everything works except I can't assign the "value" attribute to a variable. The code works correctly if I hard code the value attribute for the button. As you can see below, I've tried to modify via attr but this is not working despite verification that qnumber is returning a valid value.

How can I assign the value property of each line's edit button via a variable?

var $tablebody = $('<tbody</tbody>');
for (var i = 0; i <result.length - 1; i++) {
    var quest = result[i];
    var qnumber = quest.qnumber;
    var $row = $('<tr></tr>');
    $row.append($('<td></td>').text(quest.question));
    $row.append($('<td></td>').text(quest.a1));
    $row.append($('<td> <button name="subject" id="subject" type="submit" formaction = "test1.php" value="">Edit</button> </td>'));
    $('#subject').attr('value', qnumber);

    $tablebody.append($row);
}

1 Answer 1

1

The problem is that you add more buttons with same id and that' a wrong way because id attribute must be unique.

<button name="subject" id="subject" type="submit" formaction = "test1.php" value="">Edit</button>

Use classes instead.

Sign up to request clarification or add additional context in comments.

1 Comment

OK. I understand that having multiple buttons with the same id is the issue but I'm afraid I don't understand how to apply a class to solve this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.