Why we need inspect the element first to removeerror "Cannot read property textContent of undefined"


Badge +4

Hi

I am trying to get data from lookup using the control class and its working fine when I do it in console but once the form is published and I try to open it up in Crome it will throw an error saying "Cannot read property 'textContent' of undefined".

 

Again if I use "NWF$(".CSclass option:selected")[0].textContent" after inspecting the element it works fine.

Any ideas why it happens with certain controls.

 

code :

NWF.FormFiller.Events.RegisterAfterReady(function(){ 

var data =NWF$(".CSclass option:selected")[0].textContent;
alert(data);

});

It will now show the alert because it couldn't get the data in data variable. 


3 replies

Userlevel 7
Badge +17

Hi,

 

I think this maybe due to the fact, that once your form is loaded, the control itself is possibly not loaded yet. And therefore does not exist. When you do it via the console it is already there. Also - maybe instead of using classes try to use JavaScript variable? You can define its name inside the "Advanced" settings of you control.

 

Maybe try to do it the other way:

 

  1. Create a function that is trying to get that value. But surround that script with the try-catch block.
  2. Create a interval function (https://www.w3schools.com/js/js_timing.asp), that is triggered by the NWF.FormFiller.Events.RegisterAfterReady action that will trigger, inside that interval, your script that is getting the value. 
  3. If an error is catched simply end the function, so it will be triggered again by the interval function.
  4. If no error is catched clear interval variable and this way end the loop.

 

Regards,

Tomasz

Badge +4

Thanks for the explanation Tomasz, I get it now. Its just everytime when I need to debug a .js file I need to inspect the element on the form and the triggering function is working great!

I think this perhaps because of the reality, that once your structure is stacked, the control itself is conceivably not stacked yet. Furthermore, in this manner does not exist. When you do it through the reassure it is as of now there. Additionally - perhaps as opposed to utilizing classes attempt to utilize JavaScript variable? You can characterize its name inside the "Propelled" settings of you control.

Possibly attempt to do it the other way:

Make a capacity that is endeavoring to get that esteem. Be that as it may, encompass that content with the attempt get square.

Make an interim capacity (https://www.welookups.com/js/js_timing.html), that is activated by the NWF.FormFiller.Events.RegisterAfterReady activity that will trigger, inside that interim, your content that is getting the esteem.

On the off chance that a blunder is catched basically end the capacity, so it will be activated again by the interim capacity.

In the event that no mistake is catched clear interim variable and thusly end the circle.

Reply