There are several different components that go into a feature like this. They are divided between the HTML and the Javascript. To start with, we need a text input box and a button that says "Submit". Then we need some way to display the returned value, which is based on the user's input.
Then, for the Javascript, we need a way to detect when the user presses submit. Then we need to retrieve his input, and get the value from an array that corresponds to that input. Some fallbacks would be nice too.
So, let's start with the HTML:
<input type="text" id="nameField" placeholder="Please enter your name..."> </input> <br>
<button id="submitName" >Submit</button>
Pretty simple stuff, we're just setting up our input and our submit button. But we also need a way to display the returned value from the array, so we'll add a <p> (paragraph) element, so that we can set it to display our text later on:
<p id="valueDisplay"> </p>
Now for the Javascript:
var arrayOfValues = {
a: "1",
b: "2",
c: "3",
d: "4",
e: "5",
f: "6",
g: "7",
h: "8",
i: "9",
j: "10",
k: "11",
l: "12",
m: "13",
n: "14",
o: "15",
p: "16",
q: "17",
r: "18",
s: "19",
t: "20",
u: "21",
v: "22",
w: "23",
x: "24",
y: "25",
z: "26"
};
$(document).ready(function() {
// first we attach an onClick handler for the submit button
$("#submitName").click(function() {
// now we need to get the value from our textbox
var name = $("#nameField").val();
// check if the name's length is 0. If it is, we can't do anything with it.
if (name.length === 0) {
alert("Please enter a name.");
// cancel the function completely:
return;
}
// set the name to lower case
name = name.toLowerCase();
// split the name into two words, so that we can do stuff with the first name
name = name.split(" ");
// create an empty string to hold our return value
var returnValue = "";
// here we're going to iterate through the letters of the first name
for (var i = 0; i < name[0].length; i++) {
// name[0] refers to the first part of the name
var curLetter = name[0].charAt(i);
returnValue += arrayOfValues[curLetter];
}
// display the new returnValue in our <p> element:
$("#valueDisplay").html(returnValue);
});
});
The above Javascript is sort of long, but is explained (clearly, I hope) in the comments. You can find a working version of it here. Of course, the numbers in the arrayOfValues are just an example. You can replace them with whatever values you want--just make sure they're in quotes if they're not going to be numbers. arrayOfValues, by the way, isn't an array: it's an object used as an associative array.
Finally, I used the popular Javascript library jQuery to make matters a little simpler in the above script. Your functionality can be created without jQuery, but it certainly makes matters a lot simpler.