Skip to content Skip to sidebar Skip to footer

Fill Data In Input Boxes Automatically

I have four input boxes. If the user fills the first box and clicks a button then it should autofill the remaining input boxes with the value user input in the first box. Can it be

Solution 1:

On button click, call this function

function fillValuesInTextBoxes()
{
    var text = document.getElementById("firsttextbox").value;

    document.getElementById("secondtextbox").value = text;
    document.getElementById("thirdtextbox").value = text;
    document.getElementById("fourthtextbox").value = text;
}

Solution 2:

Yes, it's possible. For example:

 <form id="sampleForm">
     <input type="text" id="fromInput" />
     <input type="text" class="autofiller"/>
     <input type="text" class="autofiller"/>
     <input type="text" class="autofiller"/>
     <input type="button"value="Fill" id="filler" >
     <input type="button"value="Fill without jQuery" id="filler2" onClick="fillValuesNoJQuery()">
 </form>

with the javascript

 function fillValues() {
       var value = $("#fromInput").val();
       var fields= $(".autofiller");
       fields.each(function (i) {
         $(this).val(value);
       });
 }

 $("#filler").click(fillValues);

assuming you have jQuery aviable. You can see it working here: http://jsfiddle.net/ramsesoriginal/yYRkM/

Although I would like to note that you shouldn't include jQuery just for this functionality... if you already have it, it's great, but else just go with a:

 fillValuesNoJQuery = function () {
       var value = document.getElementById("fromInput").value;
       var oForm = document.getElementById("sampleForm");
       var i = 0;
       while (el = oForm.elements[i++]) if (el.className == 'autofiller') el.value= value ;
 }

You can see that in action too: http://jsfiddle.net/ramsesoriginal/yYRkM/


Solution 3:

or if input:checkbox

document.getElementById("checkbox-identifier").checked=true; //or ="checked"

Post a Comment for "Fill Data In Input Boxes Automatically"