karansys
karansys

Reputation: 2729

Accessing whole <td> of input type date value set

I want to access all the input tags that have type="date" and value of date set.

My requirement is I want to select all the input elements that have a value of date set, Input element within and , I want to access the whole that has date value set. When I click on "save" button

In the code, I just managed to display a number of input elements that have date value set. Instead, when input elements of row1 and row3 have been set value of date, I want to retrieve the whole of row1 and row3

How do I achieve this using jQuery?

let buttonsdiv;

function maintest() {
  constructTable();
  getDOMButtons();
}

function constructTable() {
  table = $('<table>');
  let row;
  let cell1;
  let cell2;
  let header2;

  table.attr({
    "id": "testTable"
  });


  row = $('<tr>');
  table.append(row);

  header2 = $('<th>').text("Feature");
  header3 = $('<th>');
  input = header3.text("Return Date/Time");
  header3.append(input);
  row.append(header2);
  row.append(header3);

  for (i = 0; i < 3; i++) {
    row = $('<tr>');
    table.append(row);


    cell1 = $('<td>').html("cell" +i);
    row.append(cell1);

    cell2 = $('<td>');
    row.append(cell2);

    input = $('<input>').attr({
      "type": "date",
      "id": "input" + i
    });
    cell2.append(input);
  }

  $("#mainDiv").append(table);
}


function getDOMButtons() {

  buttonsdiv = $("<div></div>").attr({
    "id": "buttonsdiv"
  });
  $("<button>Save</button>").attr({
      "value": "Save",
      "id": "saveButton"
    })
    .appendTo(buttonsdiv).click(function() {
      parseDOM();
    });

  $("#mainDiv").append(buttonsdiv);
}

function parseDOM() {
  $(document).ready(function() {
    let divs = $('input[type="date"]').filter((i,el) => el.value != "");;
    alert(divs.length);
  })
}
table {
  display: unset !important;
  border-collapse: collapse;
}

td,
th {
  border: 1px solid black;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body onload="maintest()">
  <div id="mainDiv"></div>
</body>

Upvotes: 0

Views: 253

Answers (2)

Ritesh Khandekar
Ritesh Khandekar

Reputation: 4015

Loop through the divs variable & alert html using:

$(this).parent().parent().html() // input < td < tr.html()

let buttonsdiv;

function maintest() {
  constructTable();
  getDOMButtons();
}

function constructTable() {
  table = $('<table>');
  let row;
  let cell1;
  let cell2;
  let header2;

  table.attr({
    "id": "testTable"
  });


  row = $('<tr>');
  table.append(row);

  header2 = $('<th>').text("Feature");
  header3 = $('<th>');
  input = header3.text("Return Date/Time");
  header3.append(input);
  row.append(header2);
  row.append(header3);

  for (i = 0; i < 3; i++) {
    row = $('<tr>');
    table.append(row);


    cell1 = $('<td>').html("cell" +i);
    row.append(cell1);

    cell2 = $('<td>');
    row.append(cell2);

    input = $('<input>').attr({
      "type": "date",
      "id": "input" + i
    });
    cell2.append(input);
  }

  $("#mainDiv").append(table);
}


function getDOMButtons() {

  buttonsdiv = $("<div></div>").attr({
    "id": "buttonsdiv"
  });
  $("<button>Save</button>").attr({
      "value": "Save",
      "id": "saveButton"
    })
    .appendTo(buttonsdiv).click(function() {
      parseDOM();
    });

  $("#mainDiv").append(buttonsdiv);
}

function parseDOM() {
  $(document).ready(function() {
    let divs = $('input[type="date"]').filter((i,el) => el.value != "");;
    divs.each(function (){
      alert($(this).parent().parent().html());
    });
  })
}
table {
  display: unset !important;
  border-collapse: collapse;
}

td,
th {
  border: 1px solid black;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body onload="maintest()">
  <div id="mainDiv"></div>
</body>

Upvotes: 1

Harsimranjit Singh
Harsimranjit Singh

Reputation: 245

If you just want to get the values of all filled date fields then this may work for you. otherwise provide some more clarification

let buttonsdiv;

function maintest() {
  constructTable();
  getDOMButtons();
}

function constructTable() {
  table = $('<table>');
  let row;
  let cell1;
  let cell2;
  let header2;

  table.attr({
    "id": "testTable"
  });


  row = $('<tr>');
  table.append(row);

  header2 = $('<th>').text("Feature");
  header3 = $('<th>');
  input = header3.text("Return Date/Time");
  header3.append(input);
  row.append(header2);
  row.append(header3);

  for (i = 0; i < 3; i++) {
    row = $('<tr>');
    table.append(row);


    cell1 = $('<td>').html("cell" +i);
    row.append(cell1);

    cell2 = $('<td>');
    row.append(cell2);

    input = $('<input>').attr({
      "type": "date",
      "id": "input" + i
    });
    cell2.append(input);
  }

  $("#mainDiv").append(table);
}


function getDOMButtons() {

  buttonsdiv = $("<div></div>").attr({
    "id": "buttonsdiv"
  });
  $("<button>Save</button>").attr({
      "value": "Save",
      "id": "saveButton"
    })
    .appendTo(buttonsdiv).click(function() {
      parseDOM();
    });

  $("#mainDiv").append(buttonsdiv);
}

function parseDOM() {
  $(document).ready(function() {
    //let divs = $('input[type="date"]').filter((i,el) => el.value != "");;
    //alert(divs.length);
   $('input[type="date"]').filter((i,el) => el.value != "").each(function(){
    alert($(this).val());
    });
  })
}
table {
  display: unset !important;
  border-collapse: collapse;
}

td,
th {
  border: 1px solid black;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body onload="maintest()">
  <div id="mainDiv"></div>
</body>

Upvotes: 1

Related Questions