简体   繁体   中英

Getting the value of a specific div inside a table row

I have a dynamic table with data from a database. Each row contains 4 div elements, with some text. I would like to be able to get the value of one of those div's.

Here is a little bit of the table:

function addData(Dato, BookedBy, Fra ,Til) {

              var tbl = document.getElementById('bookingListe');
              var lastRow = tbl.rows.length;

              // if there's no header row in the table, then iteration = lastRow + 1
              var iteration = lastRow;
              var row = tbl.insertRow(lastRow);

              // left cell
              var cellLeft = row.insertCell(0);
              var person = document.createElement("div");
              var dato = document.createElement("div");

              var fra = document.createElement("div");
              var til = document.createElement("div");
              var checkbox = document.createElement("input");
              checkbox.type = "checkbox";
              checkbox.name = "delete";


              dato.innerHTML ="Booket for: " + Dato;
              person.innerHTML ="Booket av: " + BookedBy;
              fra.innerHTML ="Fra: " + Fra;
              til.innerHTML ="Til: " + Til;


              cellLeft.appendChild(person);
              cellLeft.appendChild(dato);
              cellLeft.appendChild(checkbox);
              cellLeft.appendChild(til);
              cellLeft.appendChild(fra);

Here is the method for getting one of those divs.

function Slett(){
   $('#bookingListe').find('tr').each(function () {
      var row = $(this);
      if (row.find('input[type="checkbox"]').is(':checked')){
          alert(row.find('div').val());
        // Since there are 4 divs, the code above isn't going to work
      }
  });
 }

This isn't working, and Javascript isn't my strong side. Any suggestions are appreciated.

I would recommend you to add unique class to div like

var dato = document.createElement("div");
d.className = "dato";

Then you can access it using class selector . As dato is a div element you need to use .html() or .text()

var dataText = row.find('.dato').html();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM