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

functionfillValuesInTextBoxes()
{
    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">
     <inputtype="text" id="fromInput" />
     <inputtype="text" class="autofiller"/>
     <inputtype="text" class="autofiller"/>
     <inputtype="text" class="autofiller"/>
     <inputtype="button"value="Fill" id="filler" >
     <inputtype="button"value="Fill without jQuery" id="filler2" onClick="fillValuesNoJQuery()">
 </form>

with the javascript

functionfillValues() {
       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"