Why is my variable tabledata returning undefined?

John

What I am trying to do in my code is to return a row of my table when it is clicked. I am doing this in the jquery function $("tr.table").click(function)..... After that I am trying to store the data of this table row in a variable named tableData. This all works fine but when i try to use the variable tabledata in an if statement. I get an tabledata is undefined error. My question is how can I use the variable tabledata in my if statement without getting an error.

my javascript code

function onUpdate() {
    var tableData
    var auth2 = gapi.auth2.getAuthInstance();
    var profile = auth2.currentUser.get().getBasicProfile();

    $("tr.table").click(function () {
        tableData = $(this).children("td").map(function () {
            return $(this).text();
        }).get();
    });

    if( tableData[7] === 2) {
        console.log("if statement");
        ...
    }
    else {
        console.log("else statement");
        $.confirm({
            title: '',
            content: '',
            buttons: {
                confirm: function () {

                }
            }
        });
    }
}

my html table code

<table class="table table-bordered table-responsive-sm table-hover">
    <thead>
        <tr>
            <th>Date</th>
            <th>TimeSlot</th>
            <th>Room</th>
            <th>Attendees</th>
            <th>update</th>
            <th>delete</th>
        </tr>
    </thead>
         <tbody>
            <tr class="table">
                <td class="table">...</td>
                <td class="table">...</td>
                <td class="table">...</td>
                <td class="table">...</td>
                <td class="table" style="display: none">...</td>
                <td class="table" style="display: none">...</td>
                <td class="table" style="display: none">...</td>
                <td class="table" style="display: none">...</td>
                <td class="table command">
                    <a onclick="onUpdate()" style="color: #007bff; cursor: pointer"> <i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
                </td>
                <td class="table command">
                    <a onclick="onDelete()" style="color: #007bff; cursor: pointer"> <i class="fa fa-trash" aria-hidden="true"></i></a>
                </td>
            </tr>
        </tbody>
</table>
Pirate

You don't need to assign click event within onUpdate function. If I understood right, you want to click on update button and execute if..else block. You can pass a reference of a element like onclick="onUpdate(this)" in HTML. Then use .closest() to find the tr and get entire row as an array. One thing to keep in mide here is in if using === for comparison may not work as array will contain string value even for numbers. So either do type conversion or just use == to compare only values as done here.

function onUpdate($this) {
  var tableData;

  tableData = $($this).closest('tr').children("td").map(function() {
    return $(this).text();
  }).get();

  //console.log(tableData);
  if (tableData[7] == 2) {
    console.log("if statement 2");
  } else if (tableData[7] == 22) {
    console.log("if statement 22");
  } else {
    console.log("else statement");
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table class="table table-bordered table-responsive-sm table-hover">
  <thead>
    <tr>
      <th>Date</th>
      <th>TimeSlot</th>
      <th>Room</th>
      <th>Attendees</th>
      <th>update</th>
      <th>delete</th>
    </tr>
  </thead>
  <tbody>
    <tr class="table">
      <td class="table">...</td>
      <td class="table">...</td>
      <td class="table">...</td>
      <td class="table">value 2</td>
      <td class="table" style="display: none">...</td>
      <td class="table" style="display: none">1</td>
      <td class="table" style="display: none">3</td>
      <td class="table" style="display: none">2</td>
      <td class="table command">
        <a onclick="onUpdate(this)" style="color: #007bff; cursor: pointer"> update</a>
      </td>
      <td class="table command">
        <a onclick="onDelete()" style="color: #007bff; cursor: pointer"> delete</a>
      </td>
    </tr>
    <tr class="table">
      <td class="table">...</td>
      <td class="table">...</td>
      <td class="table">...</td>
      <td class="table">value 22</td>
      <td class="table" style="display: none">...</td>
      <td class="table" style="display: none">1</td>
      <td class="table" style="display: none">3</td>
      <td class="table" style="display: none">22</td>
      <td class="table command">
        <a onclick="onUpdate(this)" style="color: #007bff; cursor: pointer"> update</a>
      </td>
      <td class="table command">
        <a onclick="onDelete()" style="color: #007bff; cursor: pointer"> delete</a>
      </td>
    </tr>
    <tr class="table">
      <td class="table">...</td>
      <td class="table">...</td>
      <td class="table">...</td>
      <td class="table">value 33</td>
      <td class="table" style="display: none">...</td>
      <td class="table" style="display: none">1</td>
      <td class="table" style="display: none">3</td>
      <td class="table" style="display: none">33</td>
      <td class="table command">
        <a onclick="onUpdate(this)" style="color: #007bff; cursor: pointer"> update</a>
      </td>
      <td class="table command">
        <a onclick="onDelete()" style="color: #007bff; cursor: pointer"> delete</a>
      </td>
    </tr>
  </tbody>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why is my object returning undefined?

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank?

Why is my promise returning undefined?

Why is my imported variable undefined?

Why is my get request returning undefined in my image file tree?

Why does flowtype think my variable is undefined?

Why is my variable returning undefined in javascript?

Why is my reducer returning undefined, React TypeScript

Why my static variable is undefined after assigning it?

Why is my recursive function returning undefined?

Why is my EJS variable returning "undefined" when i'm trying to use the mongoose "findByIdAndRemove" method

Why is my Angular scope variable undefined?

Why is the variable argument of this required function returning undefined?

Why is my Swift string variable returning nil?

Angular Factory - Why is my variable undefined?

Why is my tableView variable returning nil?

my variable is returning empty why?

Laravel: Why is my path variable undefined?

Why is my email check variable returning an error as undefined

Why is my function returning an array with undefined as items?

Why is my pdo variable all the time undefined?

why does my functions returning "undefined"

Why is my axios post returning undefined in my functional component?

why does my nodejs function returning undefined?

My variable is always undefined, function returning before awaiting the query

Why is my intersectionObserver returning my querySelect as undefined?

Why is my function returning 1 and not the variable value?

why my Nodejs / NestJs promise is returning undefined to variable in second function?

Why is my variable returning as 'undefined' despite being set a value?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive