Skip to content

Instantly share code, notes, and snippets.

@shimo164
Created January 8, 2019 20:56
Show Gist options
  • Select an option

  • Save shimo164/c0471cd50e1eee989d23607a44b07f4c to your computer and use it in GitHub Desktop.

Select an option

Save shimo164/c0471cd50e1eee989d23607a44b07f4c to your computer and use it in GitHub Desktop.
copy, swap two forms, delete content of form
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
</head>
<body>
<script>
/*copy form id1 to id2*/
function copyForm(id1,id2){
var a=document.getElementById(id1);
var b=document.getElementById(id2);
for(var i=0;i<a.length;i++){
var n=a[i].name;
if(!b[n]) continue;
switch(a[i].type){
case "text":
case "textarea":
b[n].value=a[i].value;
break;
}
}
}
/*swap form id1 id2*/
function swapForm(id1,id2){
var a=document.getElementById(id1);
var b=document.getElementById(id2);
for(var i=0;i<a.length;i++){
var n=a[i].name;
if(!b[n]) continue;
switch(a[i].type){
case "text":
case "textarea":
var b_tmp = b[n].value;
b[n].value=a[i].value;
a[n].value=b_tmp;
break;
}
}
}
/*delete form id*/
function deleteForm(id){
var b=document.getElementById(id);
for(var i=0;i<a.length;i++){
var n=a[i].name;
if(!b[n]) continue;
switch(a[i].type){
case "text":
case "textarea":
b[n].value = '';
break;
}
}
}
</script>
<form id="a">
text1:<input type="text" name="text1" value="default1"><br>
text2:<input type="text" name="text2" value="AAA"><br>
textarea:<textarea name="comment">textarea1</textarea><br>
<input type="button" value="swap" onClick="swapForm('a','b')">
<input type="button" value="copy" onClick="copyForm('a','b')">
<input type="button" value="delete" onClick="deleteForm('a')">
</form>
<p>
<form id="b">
text1:<input type="text" name="text1" value="default2"><br>
text2:<input type="text" name="text2" value="BBB"><br>
textarea:<textarea name="comment">textarea2</textarea><br>
</form>
<input type="button" value="delete" onClick="deleteForm('b')">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment