Newbie JavaScript Array question. Please help?
-
I'm new to JavaScripting and I'm currently just trying out new things and seeing how they function to get a feel for the script syntax and functionality. With the script listed below, I was trying to create an array that would match up a person's name with their job title, and when the user entered the user's name into the text box and hit the submit button, it would pop up an alert with said person's job title. Sounds easy enough, right? Well, I've been stumped for the past five hours, and no tutorial sites have given any clue as to how I can remedy this. I'm trying to keep the literal notation (just to see how it works), but wouldn't mind a dense notation if it'll help me figure it out! What it's doing currently is just alerting the first letter of each respective job title (ex. "John" alerts "E" and "Felicia" alerts "C"). Any ideas as to how I can alter this to display the ENTIRE respective job name? Thank you in advance! Hopefully one day soon I'll be laughing at questions like this. Code (including basic body HTML): <script type="text/javascript"> var nameName = ['John', 'Robert', 'Felicia', 'Jose']; var nameJob = ['Engineer', 'Physician', 'Chef', 'Self employed']; function findJob() { var formTest = document.getElementById("person"); for(i = 0; i< nameName.length; i++) { for (j = 0; j<nameJob[i].length; j++) { if (formTest.value == nameName[i]) { alert(nameJob[i][j]); break; } } } } </script> <title>My Javascript Web Form Search</title> <style type="text/css"> input { margin:5px; align:left;} li {list-style-type: square;} </style> </head> <body> <div name="inputboxes" style="color:#F03; margin-top: 100px;"> <input type="text" id="person" size=40/> <input type="button" value="Find his/her job title!" name="findjob" onclick="findJob();" /> </div> <p> <ul> <li>John</li> <li>Robert</li> <li>Felicia</li> <li>Jose</li> </ul> </p>
-
Answer:
A cooler way to do this is to use an associative array, or as it's known in javascript an object. var jobs = { 'John': 'Engineer', 'Robert': 'Physician', 'Felicia': 'Chef', 'Jose': 'Self Employed' }; function findJob() { var personName = document.getElementById("person").value; alert(jobs[personName]); }
unibumps at Yahoo! Answers Visit the source
Other answers
Your logic doesnt make sense. Your arrays are 1 dimensional..youre trying to access nameJob[i][j] but it only has 1 dimension! function findJob() { var formTest = document.getElementById("person"); for(i = 0; i< nameName.length; i++) { if (formTest.value == nameName[i]) { alert(nameJob[i]); break; } } } With this sort of mapping, use a 2D array. This example shows EXACTLY what youre trying to job (name/job) http://www.trans4mind.com/personal_development/JavaScript/Array2D.htm
Related Q & A:
- How to get Javascript array length?Best solution by Stack Overflow
- I did something really bad and now i need help please help me.Best solution by Yahoo! Answers
- Help with TV show, please help?Best solution by Yahoo! Answers
- 10 Points!! Can You Please Help?Best solution by Yahoo! Answers
- Have any fundraising ideas? please help?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.