Update textbox with javascript?

0
Hi, I have tried to update a textbox with javascript, but it won’t work, anyone understand why this won’t work?   I have tried; document.getElementsByClassName('mytextbox')[0].value = "newvalue"; and  document.getElementById('mytextbox').value='newvalue';  
asked
2 answers
0

Hi Thomas,

you can use like this..

var $parentDiv = $('.mytextbox').closest('.mytextbox');
               $parentDiv.find('input').val('value'); //finding the inputs inside the class
               $parentDiv.find('textarea').val('value'); //finding the textarea inside the class

answered
0

At what point are you executing the code? If it's on pageload (instead of a button f.i.), then the elements are probably not loaded yet when the script is executed, and the code won't retrieve the element. If so, you can fix this by adding a small delay. 

See an example below

setTimeout(() => {  console.log("World!"); }, 2000);

 

answered