I have a form which contains two fields. The first one accepts a word. The second one is the word scrambled. Currently I have to manually scramble the word.
What I would like to do is after the first field looses focus that it can call a javascript function that automatically scrambles that word.
function scramble {
var someText = document.form.inputName.value;
var index=0;
var tempString;
usedIndexes = new Array();
for (var i=0; i<someText.length(); i++) {
index=Math.random() * someText.length();
//check if the index has not already been used - if it has, generate a new one....
tempString[i] = someText[index];
}
//then let the value in the textbox = the generated value...
document.form.inputName.value = tempString;
}
Don't know if that will work - i am but a lowly C/C++ programmer...
Regards MW
"Nothing is too true to be wonderful" - Michael Farraday (UCLA)
Looks, great. We just have to work on the Javascript part now. Anyhow, I've created my own 'scramble' script that I worked on for a few minutes, but it doesn't work yet...
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
I know this thread is really old, but I figured I'd revive it and provide a solution, since Doobster never replied saying he found one, and I never provided one. So here is working code.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Strict 1.0//EN"
"http://www.w3.org/TR/xhtml1/dtd/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JavaScript Example</title>
</head>
<body>
<h1>JavaScript Example</h1>
<script type="text/javascript">
/**********************************************************/
/*Copyright Jonathan Fenocchi */
/* http://cmm.sonoracabinets.com/jona/index.php */
/* or http://cmm.rgabbard.com/ */
/* Do not use without direct permission from Jonathan */
/**********************************************************/
function get(data){
queryStr = location.search;
queryStr = queryStr.split(data+"=")[1];
queryStr = (queryStr.indexOf("&"))?queryStr.split("&")[0]:queryStr;
return unescape(queryStr);
}
var str = get('str');
if(str.indexOf(' ') != -1){
var strs = str.split(" ");
for(j=0; j<strs.length; j++){
var str = strs[j];
var len = str.length;
var ary = new Array(len);
var rand = 0;
for(i=0; i<len; i++){
rand = Math.floor(Math.random()*str.length);
if(ary[rand] != rand){
document.write (str[rand]);
ary[rand] = rand;
} else {
len++;
}
}
document.write (" ");
}
} else {
var len = str.length;
var ary = new Array(len);
var rand = 0;
for(i=0; i<len; i++){
rand = Math.floor(Math.random()*str.length);
if(ary[rand] != rand){
document.write(str[rand]);
ary[rand] = rand;
} else {
len++;
}
}
}
</script>
</body>
</html>
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
Bookmarks