I want to make conversion tool which converts one code (user input) to another (predefined). I decided to use Javascript object as a container for codes, and my function will take user input which is actually a key from a javascript object, match it to the one in Code container and if the match is found, the function will display value to the alert box.
I made one code, but it does not work. I tried to find the solution but for now, I failed.
Here is my code:
$(document).ready(function() {
$("#convert").click(function(){
var GardinerToUnicodeCodePoint = {
"A1" :"995328",
"A1A" :"995329",
"A1B" :"995330",
"A1C" :"995331",
"A2" :"995332",
"A2A" :"995333",
"A3" :"995334",
"A3A" :"995335",
"A3B" :"995336",
"A4" :"995337",
"A4A" :"995338",
"A4B" :"995339",
"A4C" :"995340",
"A4D" :"995341",
"A4E" :"995342",
"A5" :"995343",
"A5A" :"995344",
"A5B" :"995345",
"A5C" :"995346",
"A6" :"995347",
};
var userInput = $("#userInput").val; /*for example 'A1'*/
if (userInput in GardinerToUnicodeCodePoint) {
alert(/*value of key 'userInput' -> 995328*/);
} else {
alert("No code found!");
}
});
});
Pass that function your
string input
from the user, and it'll return what you want I think.So, you're full solution would look like this:
The issue here as expresed in the comments by @epascarello is that you should use
$("#userInput").val();
with the parenthesisCode example:
You can use
[]
after calling the object to get the key value pair:Change your code to:
See jsfiddle: https://jsfiddle.net/wy70s3gj/