0

I have a table and each row has two dropdowns. I am trying to store the entire table values in an array using javascript.

I am able to fetch the values of cells[0] and cells[1] but not cells[3] and cells[4] which contain dropdowns.

Here is my javascript

    $(document).on('click', '#classTypeFetch', function (e) {
    var table = document.getElementById('tblClassificationSearchResult');
    var tableArray = [];
    for (var i = 1; i < table.rows.length; i++) {
        tableArray.push({
            charName: table.rows[i].cells[0].innerHTML,
            charDesc: table.rows[i].cells[1].innerHTML,
            parentValue: table.rows[i].cells[2].getElementById("ddlParent").value,
            childValue: table.rows[i].cells[3].getElementById("ddlParent").value,
        });
    }
    var result = tableArray;
});
Ravi
  • 326
  • 1
  • 4
  • 10
  • 2
    You can't repeat ids man. Ids must be unique for a document. As such the lookup for 'ddlParent' is most likely bad. Change those to classes and try that. – Taplar Aug 04 '17 at 14:00
  • What is the error? Does `getElementById` even work on an element like that? What does it return? If you have multiple elements with the same `id`, what do you expect it to return? – David Aug 04 '17 at 14:02
  • 1
    Optionally you could try changing it to `getElementsByTagName('select')[0].value` – Taplar Aug 04 '17 at 14:05
  • Show your `table` HTML.... 2 or 3 rows... – Louys Patrice Bessette Aug 04 '17 at 14:05
  • Anyone know if there is a S.O. specifically addressing the unique id issue? This comes up so often it seems that I'm starting to think i need to bookmark it for quick pasting into question comments... – Taplar Aug 04 '17 at 14:07
  • 2
    https://stackoverflow.com/questions/9454645/does-id-have-to-be-unique-in-the-whole-page wham – Taplar Aug 04 '17 at 14:11
  • adding indivudual ids to each element helped. Thankyou @Taplar – Ravi Aug 04 '17 at 15:04
  • Possible duplicate of [Does ID have to be unique in the whole page?](https://stackoverflow.com/questions/9454645/does-id-have-to-be-unique-in-the-whole-page) – Liam Sep 18 '19 at 09:11

0 Answers0