3 Replies - 874 Views - Last Post: 29 August 2011 - 12:29 AM

#1 arvindthakur  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 133
  • Joined: 07-June 11

how to get index of a string from a list?

Posted 25 August 2011 - 04:41 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: how to get index of a string from a list?

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

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.

Was This Post Helpful? 1
  • +
  • -

#3 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 834
  • Joined: 25-October 08

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...
<!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

Was This Post Helpful? 1
  • +
  • -

#4 arvindthakur  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 133
  • Joined: 07-June 11

Re: how to get index of a string from a list?

Posted 29 August 2011 - 12:29 AM

thanks guys ....ut really helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1