querySelector not working

querySelector not working

1j2o3y1j2o3y Posts: 7Questions: 1Answers: 0

test case at

https://live.datatables.net/yebelasu/1/edit

here trying to use the below code

var oddtd = document.querySelectorAll('table.display tbody tr:nth-child(odd) td');

oddtd.innerHTML = 'some other value';

but nothing changes.

here trying to get 'some other value' in each odd rows td cells.

any solution with same code like document.querySelectorAll ?

Answers

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    I'm not clear what you're trying to achieve here. If you change the HTML of the table once DataTables has been initialised, it means that DataTables isn't aware of that change so can't factor that value in to any searching or ordering results, plus as soon as the table is redrawn, say because the page changes, that data will be lost.

    If you want to change the data once the table has been initialised, use the API methods, such as cell().data(), row().data(), or column.data(),

    Colin

  • 1j2o3y1j2o3y Posts: 7Questions: 1Answers: 0

    So it is not possible to interact with the table with querySelectorAll ?

  • 1j2o3y1j2o3y Posts: 7Questions: 1Answers: 0

    What is the correct way to mention all even rows as a variable ?

    Is it the below following is right ?

    vat treven = table.rows (even);

  • 1j2o3y1j2o3y Posts: 7Questions: 1Answers: 0

    Or how to mention it with querySelectorAll ?

    like document.querySelectorAll('.display tr:nth-child (even')

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    You can interact with the table using QSA, but you'll run into a bunch of problems - e.g.:

    • If you attempt to update the data, DataTables wouldn't see the modification
    • You wouldn't be able to select rows which are not on the current page (we remove them for performance
    • Likewise with hidden columns.

    Use rows().nodes() if you want access to the tr elements for the table.

    What is it you actually want to do?

    Allan

  • 1j2o3y1j2o3y Posts: 7Questions: 1Answers: 0

    Looking to access like this as mentioned above

    What is the correct way to mention all even rows as a variable ?

    Is it the below following is right ?

    vat treven = table.rows (even);

    Or how to mention it with querySelectorAll ?

    like document.querySelectorAll('.display tr:nth-child (even')

    hope it is clear now ?

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    It would help here if you explain what you're trying to do, as Allan and I are explaining what you're trying to do probably isn't the correct way to do it. Do you just want to style odd/even rows? If so, you can use createdRow for that (there's an example on the link doing just that).

    If you're trying to do something else, please let us know and we can advise.

    Colin

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Just to add to what Colin has said, if you simply want even rows use:

    table.rows(':nth-child(even)').nodes()
    

    See the row-selector documentation for all the selectors you can use.

    Note that you will most likely want to account for ordering and filtering as well - the selector-modifier is used for that - e.g.:

    table
      .rows(':nth-child(even)', {
        order: 'applied'
      })
      .nodes();
    

    And keep in mind that order can change as the user interacts with the table.

    So yes, please tell us what your intention is, and we can help point you in the right direction, rather than just guessing.

    Allan

  • 1j2o3y1j2o3y Posts: 7Questions: 1Answers: 0

    there is no hidden secrets there as you guess something like.

    The questions was clearly mention two times as above.

    and that's truth as well - written over above 2 times.

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Fair enough. We've supplied answers to your questions. Use the API.

    Allan

  • 1j2o3y1j2o3y Posts: 7Questions: 1Answers: 0

    cheers

Sign In or Register to comment.