13 Replies - 450 Views - Last Post: 25 July 2019 - 07:01 AM Rate Topic: -----

#1 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,165
  • Joined: 12-January 10

javascript cant find td?

Posted 24 July 2019 - 05:44 AM

i have attached an image.

i am trying to get the value of td (1) or the 2nd td ---so when traversing the elements -- it is saying that td length is 0

var reasonTb = document.getElementById("MainContent_reasonsTable");
    var reasonTbvRowLength = reasonTb.getElementsByTagName("tr");
  
    
    for (var i = 1; i < reasonTbvRowLength.length; i++) {
       
       alert('reason rw ltght ' + reasonTbvRowLength.length);   <-- shows 2
        
        alert('td length ' + reasonTbvRowLength[i].getElementsByTagName("td").length);  <--shows zero but oyu can clearly see inthe image there are multiple


        if (reasonTbvRowLength[i].getElementsByTagName("td").length  > 0) {    <-- never goes in because of zero
            alert('inner' + document.getElementById("MainContent_reasonsTable").rows[i].cells[1].value);
          //  var ctl = parseFloat(reasonTbvRowLength[i].cells[1].getElementsByTagName("input")[0].value.replace(',', ''));
           //var ctl = document.getElementById('MainContent_txtReasonMinutes' + i);
           // alert(ctl.value);
          //  alert('test ' + ctl.value);

         
        }
       


    }





thoughts?

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: javascript cant find td?

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2906
  • View blog
  • Posts: 11,325
  • Joined: 03-December 12

Re: javascript cant find td?

Posted 24 July 2019 - 06:22 AM

https://jsfiddle.net/L8dehuyw/
Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,165
  • Joined: 12-January 10

Re: javascript cant find td?

Posted 24 July 2019 - 10:52 AM

thanks for that fiddle however mine still shows 0

Attached image(s)

  • Attached Image
  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2906
  • View blog
  • Posts: 11,325
  • Joined: 03-December 12

Re: javascript cant find td?

Posted 24 July 2019 - 10:56 AM

throw it in a fiddle so i can play with it
Was This Post Helpful? 0
  • +
  • -

#5 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,165
  • Joined: 12-January 10

Re: javascript cant find td?

Posted 24 July 2019 - 11:06 AM

fiddle
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15160
  • View blog
  • Posts: 60,694
  • Joined: 12-June 08

Re: javascript cant find td?

Posted 24 July 2019 - 11:27 AM

Weird.. if you swap that for loop start value from 1 to 0 it gets some results.
Was This Post Helpful? 0
  • +
  • -

#7 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,165
  • Joined: 12-January 10

Re: javascript cant find td?

Posted 24 July 2019 - 11:31 AM

yeah but it still doesnt get the value or the td i need-- im at a loss to why it is doing that
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2906
  • View blog
  • Posts: 11,325
  • Joined: 03-December 12

Re: javascript cant find td?

Posted 24 July 2019 - 11:35 AM

What modi said. You are putting the table head at the bottom rather than the top. It finds the first table row, it just skips it since you start the loop at 1 not 0. Schematically, the head comes first when it does the iteration. if you changed the td look to th, it works exactly as you would expect.

Rather than loop through that way, why not just iterate through the controls and get the values like that?
Was This Post Helpful? 0
  • +
  • -

#9 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,165
  • Joined: 12-January 10

Re: javascript cant find td?

Posted 24 July 2019 - 11:35 AM

yeah as for the header -- i am not sure why it builds on the bottom -- i have the code set as follows:

Protected sub onRowCreated()

            Dim header as TableHeaderRow = new TableHeaderRow()
            Dim tableHeaderCell As TableHeaderCell = new TableHeaderCell()
            Dim tableHeaderCell2 As TableHeaderCell = new TableHeaderCell()
            Dim tableHeaderCell3 As TableHeaderCell = new TableHeaderCell()

            if Session("EnglishSpanish") = "English"  or Session("EnglishSpanish") Is nothing  then
                tableHeaderCell.text = "Reason"
                tableHeaderCell2.text = "Minutes"
                tableHeaderCell3.Text = "Notes"
            else
                tableHeaderCell.text = "Rasones"
                tableHeaderCell2.text = "Minutos"
                tableHeaderCell3.Text = "Notas"
            end if


            header.Cells.Add(tableHeaderCell)
            header.Cells.Add(tableHeaderCell2)
            header.Cells.Add(tableHeaderCell3)
            reasonsTable.Controls.Add(header)

    End sub

Was This Post Helpful? 0
  • +
  • -

#10 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,165
  • Joined: 12-January 10

Re: javascript cant find td?

Posted 24 July 2019 - 11:47 AM

let me make some mods trying to get the headers to work and then we can get back to this... thanks guys
Was This Post Helpful? 0
  • +
  • -

#11 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2906
  • View blog
  • Posts: 11,325
  • Joined: 03-December 12

Re: javascript cant find td?

Posted 24 July 2019 - 11:50 AM

if you change it to querySelector, you can skip over that. Then you just need to start at 0 for the loop.


 var reasonTb = document.querySelector("#MainContent_reasonsTable >tbody");

Was This Post Helpful? 0
  • +
  • -

#12 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2906
  • View blog
  • Posts: 11,325
  • Joined: 03-December 12

Re: javascript cant find td?

Posted 24 July 2019 - 11:58 AM

 var reasonTb = document.querySelector("#MainContent_reasonsTable > tbody");
    var reasonTbvRowLength = reasonTb.getElementsByTagName("tr");
    var tdDT = document.getElementById("MainContent_DTMinutes");
    
    for (var i = 0; i < reasonTbvRowLength.length; i++) {
        alert('reason rw ltght ' + reasonTbvRowLength.length);

        alert('td length ' + reasonTbvRowLength[i].getElementsByTagName("td").length);

        if (reasonTbvRowLength[i].getElementsByTagName("td").length > 0) {
            alert('inner ' + reasonTb.rows[i].cells[1].children[0].value);
           // alert('inner ' + document.getElementById("MainContent_reasonsTable").rows[i].cells[1].value);
           var ctl = parseFloat(reasonTbvRowLength[i].cells[1].getElementsByTagName("input")[0].value.replace(',', ''));
           //var ctl = document.getElementById('MainContent_txtReasonMinutes' + i);
           // alert(ctl.value);
          //  alert('test ' + ctl.value);

            if (ctl == null) {
                alert(ctl.value);
                return dtTotal;
            
            } else {
                dtTotal = dtTotal + ctl.value;
                roundedDTMinutes = Math.round(dtTotal);
                alert('sum dt' + roundedDTMinutes);
              
            }

        }
       


    }



Was This Post Helpful? 0
  • +
  • -

#13 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,165
  • Joined: 12-January 10

Re: javascript cant find td?

Posted 25 July 2019 - 05:52 AM

so i just remembered why the header is at the bottom -- because that table can have an unlimited amount of dynamically added rows -- and in order for the rows to work you need to have an index running on it -- if i hard code a header row the index doesnt work-- it was a pain in the ass to get the stupid row thing to work since the controls must be rebuilt on every row add
Was This Post Helpful? 0
  • +
  • -

#14 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,165
  • Joined: 12-January 10

Re: javascript cant find td?

Posted 25 July 2019 - 07:01 AM

so apparently this exists:

 reasonsTable.Controls.AddAt(0,header)



so i changed
 reasonsTable.Controls.Add(header)

to the above and now my headers are in the right area
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1