Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
JavaScript
var permArr = [], usedChars = [];
function permute(input) {
   // input = document.getElementById("inputText1").value.split(",");
    
    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;
}
//};
//document.write(JSON.stringify(permute([5, 3, 7, 1])));

function parseAndpermute() {
    var vals = document.getElementById("inputText1").value.split(",");
    document.write(JSON.stringify(permute(vals)));

    //permute(vals);
    document.getElementById("output").innerHTML = permArr;
    
}


HTML
<!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 :

JavaScript
<input id="inputText" type="text" />
 <input id="PermutationBtn" type="button" value="Permutation" onclick="parseAndpermute()" />
Posted
Updated 8-Oct-18 18:05pm
v8
Comments
Mohibur Rashid 8-Oct-18 10:07am    
have you declared an input box with the id inputText?
VaiShankar 8-Oct-18 11:00am    
Yes. i have used input id for both the textbox and button(with onclick permute()) in the html file.
Mohibur Rashid 8-Oct-18 17:13pm    
You are calling permute function without parameters. Why do you want to split by comma? If you expect user to input comma separated value then you need to guarantee that comma separated value is there. Anyway, in your onclick method in button you need to add the above mentioned command
VaiShankar 8-Oct-18 18:13pm    
Thanks for reply. Yes, I want to split by commas. But in onClick, what code should I write? Where should I write code to accept input from textbox? I am new to javascript. I am trying my best to understand. Sorry if my question sounded too dumb.
Mohibur Rashid 8-Oct-18 18:39pm    
Create another function, name it parseAndPermute,
inside the function write your command
var vals = document.getElementById("inputText").value.split(",");
permute(vals);

and onclick, change permute() to parseAndPermute()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900