textinput - Javascript: How to use same button for different text input? -


suppose have 2 input elements 1 button. want button function 1 input element @ time according 1 being focused don't know how capture focusing status of each element.

please consider example:

<head> <script type="text/javascript"> var id_box = document.createelement('input'); id_box.id = 'id_box'; id_box.type = 'text'; div.appendchild(id_box);  var weight_box = document.createelement('input'); weight_box.id = 'weight_box'; weight_box.type = 'text'; div.appendchild(weight_box);  function showletter() {     if (id_box being focused){         document.getelementbyid('id_box').value = 'abc';     }     if (weight_box being focused){         document.getelementbyid('weight_box').value = 'abc';     } } </script> </head>  <body>     <button onclick="showletter()">abc</button> </body> 

any idea? thank much.

if input has focus , if user clicks button input lose focus instantly cannot detect input has focus in way want.

you create variable stores "current" input attaching event handler each input onfocus event.

demo on jsfiddle: http://jsfiddle.net/p58sx/1/

var current_input;  function showletter() {   current_input.value = 'abc'; }  function setcurrentinput() {   console.log(this);   current_input = this; }  var id_box = document.createelement('input'); id_box.id = 'id_box'; id_box.type = 'text'; id_box.addeventlistener("focus", setcurrentinput); div.appendchild(id_box);  var weight_box = document.createelement('input'); weight_box.id = 'weight_box'; weight_box.type = 'text'; weight_box.addeventlistener("focus", setcurrentinput); div.appendchild(weight_box); 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -