![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2008
Posts: 35
Rep Power: 0
![]() |
Compare Input from Array Values
Hi! How do I compare user input from the values in my array and get its index?
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Mar 2005
Posts: 778
Rep Power: 6
![]() |
Re: Compare Input from Array Values
Just loop through the Array from 0 to N (where N is the size of the array) and when you find a match, you know the index.
At least that's a way to do it (don't know Javascript so can't say explicitly how to do it in Javascript)
__________________
I can remember, back in '22 They changed the law - came knocking on the door In that same moment, the broadband seemed to go.. Phones all dead. Gone dizzy in the head.. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2008
Posts: 35
Rep Power: 0
![]() |
Re: Compare Input from Array Values
wow, thanks!
![]() |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Dec 2006
Posts: 119
Rep Power: 4
![]() |
Re: Compare Input from Array Values
This is pretty sloppy but you'll get the general idea....Something like:
function searchArray()
{
var array = new Array(1,2,3,4,5,6);
//matchValue would probably come from a field input
var matchValue = 3;
var i;
for(i = 0; i < array.length; i++)
{
if(matchValue == array[i])
{
//exit the loop or call another method
matchExists();
}
}
}
function matchExists()
{
//A match exists!!!
} |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Dec 2006
Posts: 119
Rep Power: 4
![]() |
Re: Compare Input from Array Values
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2009
Posts: 6
Rep Power: 0
![]() |
Re: Compare Input from Array Values
Here's another DEMO of comparing userInput against your array lists.
javascript Syntax (Toggle Plain Text)
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Oct 2008
Posts: 35
Rep Power: 0
![]() |
Re: Compare Input from Array Values
Thanks! That's really helpful.
|
|
|
|
|
|
#8 |
|
Information Systems *****
|
Re: Compare Input from Array Values
essential,
Great post! One Question... How could you get that to work when the user hits return also, instead of just onclick? |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Apr 2009
Posts: 6
Rep Power: 0
![]() |
Re: Compare Input from Array Values
kn0wl3g3,
I would be willing to help you if you can provide me with a more specific question... essential |
|
|
|
|
|
#10 | |
|
Information Systems *****
|
Re: Compare Input from Array Values
Quote:
What my question was referring to was that it only works when the user clicks the button with the mouse, instead of hitting return on the keyboard. I found a fix using an onsubmit event also, but was wondering if you had a different solution, since it needs to have the call in two places to do it this way |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting char values from an array using pointers | IceCold19 | C | 2 | Jan 6th, 2009 4:29 PM |
| default values in input box not visible | sk123 | HTML / XHTML / CSS | 4 | Oct 21st, 2008 7:39 PM |
| Reading character input into an array (raw mode) | shoeyfighter | C | 3 | Nov 2nd, 2006 3:49 PM |
| Combining Values in an array | bigmitch | C++ | 4 | Feb 10th, 2006 7:29 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |