Milad Xandi
Milad Xandi

Reputation: 107

Getting value of enclosed inputs with cells of a table with jquery

I have a td like this:

<td> <input type="text"id="Collection" name="Collection" value="***"> </td>

I want to access the *** values to export the table with this jquery code to XML:

$("#AdExportXML").click(function () {
    var xml = "<?xml version="1.0"?>\n<Questions>\n";
        $("#Questions tr").each(function () {
            var cells = $("td", this);
            if (cells.length > 0) {
                xml += "    <Question>\n";
                xml += "      <Collection>" + cells.eq(2).html() + "</Collection>\n";
                xml += "    </Question>\n";
            }
        });
        xml += "\n</Questions>\n";

    console.log(xml);
    var blob = new Blob([xml], {type: "text/plain;charset=utf-8"});
    saveAs(blob, Date.now()+".xml");

});

Now, the given value is the entire html:

<input type="text"id="Collection" name="Collection" value="***">

How can I access its value especially?

Upvotes: 0

Views: 34

Answers (1)

Nawed Khan
Nawed Khan

Reputation: 4401

As Taplar pointed out... the cells.eq(2) is referencing to the td. You need to now refer to the input field inside the td.

xml += "      <Collection>" + cells.eq(2).find('input').val() + "</Collection>\n";

Upvotes: 1

Related Questions