I have a list like
_Value1 = "'apple','ball','cat'....so on";
If I know that apple exists in above list.How to get index of whole string from the list.Like apple should have the index 1, ball should have 2 and so on.
What is the javascript code for this stuff?
how to get index of a string from a list?
Page 1 of 13 Replies - 441 Views - Last Post: 29 August 2011 - 12:29 AM
Replies To: how to get index of a string from a list?
#2
Re: how to get index of a string from a list?
Posted 25 August 2011 - 05:46 AM
You're going to have to split the string in order to get an Array.
- Replace the single quotes.
- Split the string by a commas.
- Iterate through the string until you've reached the value you're looking for. Keep count of the amount of iterations you do also.
#3
Re: how to get index of a string from a list?
Posted 26 August 2011 - 05:52 PM
As an alternate solution (based on limited requirement information)
to 'codeprada's suggestion, you might start from something like this...
Alternatively, to avoid the "RETURN" key alert problem...
to 'codeprada's suggestion, you might start from something like this...
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
var _Value1 = "apple,ball,cat,kitten,puppy,dog,skunk,donkey,ass";
var tarr = _Value1.split(',');
alert(tarr.join('\n')); // for testing purposes
function lookForKey(v) {
document.getElementById('ndxSearch').value = '';
if ((v < 1) || (v > 9)) { alert('Out of range entry');}
else { alert('Value of index #'+v+' is: '+tarr[v-1]); }
return;
}
</script>
</head>
<body>
Value of index position (1-9):
<input type="text" size="1" value="" id="ndxSearch" onkeyup="lookForKey(this.value)">
</body>
</html>
Alternatively, to avoid the "RETURN" key alert problem...
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
function GetChar (event){
var keyCode = ('which' in event) ? event.which : event.keyCode;
if (keyCode == 13) { return; }
var v = keyCode-48;
document.getElementById('ndxSearch').value = '';
if ((v < 1) || (v > 9)) { alert('Out of range entry');}
else { alert('Value of index #'+v+' is: '+tarr[v-1]); }
return;
}
var _Value1 = "apple,ball,cat,kitten,puppy,dog,skunk,donkey,ass";
var tarr = _Value1.split(',');
alert(tarr.join('\n')); // for testing purposes
</script>
</head>
<body>
Value of index position (1-9):
<input type="text" size="1" value="" id="ndxSearch" onkeyup="GetChar(event)">
</body>
</html>
This post has been edited by JMRKER: 26 August 2011 - 06:42 PM
#4
Re: how to get index of a string from a list?
Posted 29 August 2011 - 12:29 AM
thanks guys ....ut really helps
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote




|