-2

"cousin2" should show up as 79,273,859 in the console, because that is what that value equals. But I do now know to how to change the the value after a click function. Other sources said that establishing var cousin2 as a global variable should do the trick, but it isn't.

var cousin2 = 0;

$(".checkBox").on("click", function(){

   var cousin =  $(this).parent().siblings()[1];

    cousin2 = $( cousin ).find(".tabulator-cell").prevObject[0].innerText;


});

console.log(cousin2);
Vamsi Krishna
  • 69
  • 1
  • 9
  • 1
    Have a think about what your code is doing. When your last line calls console.log, have you clicked the checkbox yet in the browser? No. So why, before you click, would you expect it to have the value that it will have after you click? – freedomn-m Oct 10 '19 at 15:48
  • You have 2 console.log(). The last line will show 0 as explained by @freedomn-m but once you click the checkbox it should call the first console.log() and it should print the correct value. Is it not? – Nawed Khan Oct 10 '19 at 15:55
  • I just deleted the first console.log (I edited it). I actually need the last one to say 79,273,859. So are you guys saying it is not possible to get that value in the console? – Vamsi Krishna Oct 10 '19 at 15:56
  • 1
    How can it be possible when your code says change the value on click but you are checking without a click! – Nawed Khan Oct 10 '19 at 15:58
  • 2
    It is time to learn about asynchronous programming and how other statements do not update after they have run. The console log line after the click event handler is run BEFORE the click event occurs. So that is never going to get the value from inside of the click. The code that needs the updated value needs to be triggered from within the click event listener. So you need to rethink whatever you coded. Since there is little to go off of on this question, we do not know exactly what the best solution is to help you. – epascarello Oct 10 '19 at 16:04
  • Here's what your code says: 1 set var to 0. 2 *setup* an event handler to be run *at a later time* when the user clicks. 3 show var value. 4 [when user clicks] set the var to new value. Just look in the console at when your outputs appear (make them different to it's clear). – freedomn-m Oct 10 '19 at 16:35
  • It is not safe to try and directly manipulate or traverse the DOM inside of a Tabulator element. It uses a virtual DOM so only the rows that are currently visible exist, once they are scrolled out of view, they are destroyed. You should be able to do this using pure JavaScript without traversing the DOM, if you can link to a JS fiddle i can point you in the right direction – Oli Folkerd Oct 15 '19 at 20:48

2 Answers2

0

You should check for the value of cousin2 INSIDE the click handler because that is when the value is changing.

var cousin2 = 0;

$(".checkBox").on("click", function(){
   var cousin =  $(this).parent().siblings()[1];

   cousin2 = $( cousin ).find(".tabulator-cell").prevObject[0].innerText;
   console.log(cousin2);
});
Nawed Khan
  • 4,393
  • 1
  • 10
  • 22
  • No the value is not changing after the click function. – Vamsi Krishna Oct 10 '19 at 16:02
  • What does it log in console after you click? I do not know what is in the innerText of your cell. I am assuming that cousin2 already have the correct value and your question is why it was not logging on console. – Nawed Khan Oct 10 '19 at 16:05
  • Then it must be 0. As I said I do not know what is in the inner text of your element, maybe share the html for that. add a line before console.log that says cousin2=101; and then upon click it should log 101. this means you are not reading the value correctly. – Nawed Khan Oct 10 '19 at 16:11
0
var cousin2 = '0';

$(".checkBox").on("click", function(){
    var cousin =  $(this).parent().siblings()[1];
    cousin2 = $( cousin ).find(".tabulator-cell").prevObject[0].html();
    console.log(cousin2);
});