0

So I'm working on a site with one main page containing a table and buttons to control the selected rows in the table. Each row in the table has a button with either a + or a - depending on whether the user has clicked it or not. It starts as a +, then when clicked it adds an identifier unique to that row to an array. The buttons have an ID equal to the unique identifier.

I want the table to auto-refresh every 10 seconds, but when I do, it resets all buttons to + because that's what is in the HTML.

I'm trying to get it to reset the already selected ones to minuses based on the array with this code:

function refreshTable(){
  $('#tablefill').load('table.php', function(){
       setTimeout(refreshTable, 10000);
    });
    $.each(selected, function(index, value) {
       document.getElementById(value).innerHTML = '-';
    });
}

I'm not too experienced with jQuery, so I'm sure I'm just missing something. Let me know if I should include any other code.

5
  • What you passing as selected? Commented Aug 2, 2013 at 21:07
  • Why document.getElementById(value).innerHTML if you can $('#' + value).html(...) ? Commented Aug 2, 2013 at 21:14
  • selected is my array of currently selected rows. The values are the unique identifiers I mentioned. The values are equal to the IDs of the buttons. Commented Aug 2, 2013 at 21:15
  • if the array is an array of rows, value will be each row, not a unique identifier. Please clarify. Commented Aug 2, 2013 at 21:18
  • Sorry, I worded that wrong. 'selected' is an array whose values are the unique id for each button. For example: selected[0] = 1234 and the button's ID is 1234. Then selected[1] = 12345 and the button the next row down on the table's id would be 12345. Sorry if that's confusing. I'm not sure how else to explain it. Commented Aug 2, 2013 at 21:22

2 Answers 2

1

Your jQuery code actually looks ok to me. I think your problem is that you're executing your .each immediately after you've called the server, not after the server has returned; the .each should be inside the callback function you've defined aboved, immediately above or below your setTimeout call, i.e.:

function refreshTable(){
  $('#tablefill').load('table.php', function(){
      $.each(selected, function(index, value) {
        document.getElementById(value).innerHTML = '-';
      });
      setTimeout(refreshTable, 10000);
  });
}
Sign up to request clarification or add additional context in comments.

Comments

0
selected.html('-');

should just work fine, assuming selected is the result of somthing like $('.yourClass');

In jQuery nearly every method works on a collection as well as on single items. No need for looping over the items.

Comments

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.