Hi, this is a Permutation program in javascript. I am struggling to find a way to use input textbox to accept values and display them. I don't know where to use the "document.getElementById("inputText").value.split(",")."
For eg: Textbox: abc.
Output: abc,acb,bac,bca,cab,cba.
Thanks in advance.
var permArr = [], usedChars = [];
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr;
}
function parseAndpermute() {
var vals = document.getElementById("inputText1").value.split(",");
document.write(JSON.stringify(permute(vals)));
document.getElementById("output").innerHTML = permArr;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input id="inputText1" type="text" />
<br /><br />
<input id="PermutationBtn" type="button" value="Permutation" onclick="parseAndpermute()" />
<br /><br />
<div id="output"></div>
<script src="js/Permutation_7985.js"></script>
</body>
</html>
What I have tried:
So far I was able to print the output if I already have values in the code and not using a input textBox.
In the html file, i have entered :
<input id="inputText" type="text" />
<input id="PermutationBtn" type="button" value="Permutation" onclick="parseAndpermute()" />