cargar una funcion javascript al valor de un input html in javascript

To load a JavaScript function into an HTML input value, you can use the value attribute of the input element to set the initial value and then use JavaScript to modify the value by calling a separate function.

Here's an example:

<input type="text" id="myInput" value="initial value">
<button onclick="setValue()">Set value</button>

<script>
function setValue() {
  var input = document.getElementById("myInput");
  input.value = myFunction(); // calls the function and sets the input value
}

function myFunction() {
  return "new value";
}
</script>
323 chars
14 lines

In this example, the setValue() function is called when the button is clicked. It gets the input element using document.getElementById(), calls the myFunction() function to get a new value, and sets the input's value to that new value.

You can replace the myFunction() function with any other function that returns the value you want to set.

related categories

gistlibby LogSnag