He intentado encontrar una forma de llamar a una función cuando cambia el valor de la entrada, pero hasta ahora no he encontrado nada. Todas las cosas que probé parecían funcionar, pero no lo hicieron. HTML:
var funds = 500; document.getElementById("submit").onclick = function() { } function AP() { if (document.getElementById("p").checked) { document.getElementById("AP").innerHTML = "%"; } else { document.getElementById("AP").innerHTML = ""; } } //right here I'd like the function to call. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Rng Crypto</title> </head> <body> <header> <h1>Crypto ran from randomness!</h1> </header> <div> <input type="radio" name="AP" id="a" onchange="AP()" checked>Absolute<input type="radio" name="AP" id="p" onchange="AP()">Percent<br> <input type="number" id="input" HERE TO ADD THINGY> <p id="AP" style="display:inline;"></p><br> <button id="submit">Submit</button> </div> <script src="RngCrypto.js"></script> </body> </html>Las cosas que he probado son:
<input type="number" id="input" onchange="input()"> <input type="number" id="input" oninput="input()"> <input type="number" id="input" onkeyup="input()"> document.getElementById("input").onchange=input(); document.getElementById("input").oninput=input(); const inputEle = document.querySelector("#input"); inputEle.addEventListener('input', function(e) { console.log(e.target.value); }) <input type="text" id="input"> ¿Ha intentado agregar el evento 'cambiar' en el elemento de entrada?
Editar: agregar eventListener 'input', es una forma más de lograr este resultado.
(consulte el siguiente código)
var funds = 500; document.getElementById("submit").onclick = function() { } function AP() { if (document.getElementById("p").checked) { document.getElementById("AP").innerHTML = "%"; } else { document.getElementById("AP").innerHTML = ""; } } //right here I'd like the function to call. document.queryselector("#input").addEventlistener('change', function(e) { console.log(e.target.value;) })Cuando te entendí bien, esto es básicamente lo que estás buscando:
<input class="js-radio-button" type="radio" name="ab" value="absolute"> Absolute<br> <input class="js-radio-button" type="radio" name="ab" value="percent"> Percent<br> <button class="js-check-selection">CHECK</button> <div> <span>Result is:</span> <span id="result"></span> </div>en tu javascript tienes:
function checkSelectedRadio() { // get your radios having the name 'ab' const radios = document.querySelectorAll('input[type=radio][name=ab]'); // reset result container document.getElementById('result').innerHTML = ''; // loop through the radios for (let i = 0; i < radios.length; i += 1) { // check for each radio if it was selected if (radios[i].checked) { // set the value of the selected radio to your result container document.getElementById('result').innerHTML = `Value: ${radios[i].value}`; // if you need more logic: if (radios[i].value === 'absolute') { // do something here if 'absolute' was checked } else if (radios[i].value === 'percent') { // do something here if 'percent' was checked } } } } // get your button to check radio status like this or fire the function above by your onchange handler const checkButton = document.querySelector('.js-check-selection'); checkButton.addEventListener('click', checkSelectedRadio);EDITAR después de leer tu comentario:
Para detectar el cambio de su campo de entrada, funciona así:
<input type="number" id="input" value="1">En tu JS:
const field = document.querySelector('#input'); function inputCheck() { console.log('input changed'); // or do something else } field.addEventListener('change', inputCheck);