Eventos en JS

/* <form method="post"> Introduce código postal: <input type="text" id="codigo"/><br/> Introduce estado y ciudad: <input type="text" id="ciudad"/> <br/> </form><br/> <button id="btn11">Localizar en Javascript</button> */ const button = document.getElementsByTagName("button"); for(var i=0; i < button.length; i++){ if(button[i].getAttribute("id")=="btn11"){ button[i].addEventListener("click",()=>{ var input = document.getElementsByTagName("input"); var codigo=""; var ciudad=""; for(var j=0; j < input.length; j++){ if(input[j].getAttribute("id")=="codigo"){ codigo=input[j].value; } if(input[j].getAttribute("id")=="ciudad"){ ciudad=input[j].value; } } alert("Codigo: "+codigo+" ,Ciudad: "+ciudad); }); } }
Acceder alementos de un form con Javascript nativo.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.