Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying value from one input field to another input field

I need a javascript function for copying the value of one input field to another input field based on checkbox selection. However i did some javascript code on click,

<script>    
function copyTextValue() {
    var text1 = document.getElementById("Name1").value;
    document.getElementById("Name2").value = text1;
    document.getElementById("Name3").value=text1;
}
</script>

<input type="checkbox" name="check1" onclick="copyTextValue();"/>

Now i need to delete the copied values in those two boxes on uncheck. I stuck up with this. Any help?

like image 837
steeve Avatar asked Jul 26 '12 05:07

steeve


1 Answers

function copyTextValue(bf) {
  var text1 = bf.checked ? document.getElementById("Name1").value : '';
  document.getElementById("Name2").value = text1;
  document.getElementById("Name3").value = text1;
}
<input type="checkbox" name="check1" onchange="copyTextValue(this);" />
<input id="Name1"><input id="Name2"><input id="Name3">
like image 99
bugwheels94 Avatar answered Nov 10 '22 13:11

bugwheels94