Juan Torres Juan Torres - 5 months ago 12
HTML Question

JavaScript event not executed after manually modifying HTML field

i have an input text field that is automatically populated whenever a user clicks on a specific button , however if the user manually changes or deletes the text from the field , then function no longer works whenever button is re-clicked !

HTML code for the button and input field:

<button id="idButton4" onclick="setTimeUnix('idInput4')" type="button">Stop2 </button><input id="idInput4" type ="text" value="0" >


the JavaScript function that should be re-executed:

function getUnixTime(){
var date = new Date();
var unixTime= date.getTime();
var datepack=[unixTime,date];
return datepack;
}

function setTimeUnix(elementId){
var datepack=getUnixTime();
var element=document.getElementById(elementId);
element.setAttribute("value",datepack[0]);}

Answer

instead of the code

element.setAttribute("value",datepack[0]);

try using

element.value = datepack[0];