2
<html>
<head>
  <script src=".\jquery.1.4.2.js"></script>
  <link rel="stylesheet" enter code heref=".\jquery.autocomplete.css" type="text/css" />   
  <script type="text/javascript" src=".\jquery.autocomplete.js"></script>

  <script>
  $(document).ready(function(){
      var data = "AndraPradesh;ArunachalPradesh;Assam;Bihar;Chhattisgarh;Goa;Gujarat;Haryana;HimachalPradesh;Jammuan&Kashmir;Jharkhand;Karnataka;Kerala;MadyaPradesh;Maharashtra;Manipur;Meghalaya;Mizoram;Nagala;Orissa;Punjab;Rajasthan;Sikkim;TamilNadu;Tripura;Uttaranchal;UttarPradesh;WestBengal".split(";");

   $("#example").autocomplete((data),{ matchContains: 0, minChars: 1, scroll: true, width: 143, selectFirst: true, multiple: true, multipleSeparator: ';', autoFill: false });
});
  </script>

</head>
<body>
<b><font color="red">ENTER INDIAN 'STATE' NAME TO SEE AUTOCOMPLETE</font></b>
<br><br> Enter States <input id="example" /> <button onclick="javascript:displaycontent();">click me</button>
</body>
<script>
function displaycontent()
{
var alertmsg = $("#example")[0].value;
alert(alertmsg );
}
</script>
</html>

I have use this above code ....once i enter the state name....it should not appear in the next attempt when we are entering another sate name

vivek salve
  • 991
  • 1
  • 9
  • 20
Raj
  • 141
  • 2
  • 10
  • 1
    Why not remove the duplicates from the array(var dataArray = data.split(";");) before http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – henkieee Oct 26 '12 at 12:14
  • var data is not an array..So how can we do? – Raj Oct 26 '12 at 12:26
  • after the word array, I described how to make a array of the data string – henkieee Oct 26 '12 at 12:32

1 Answers1

5

Ill elaborate henkieee's comment:

var data = "AndraPradesh;ArunachalPradesh;Assam;Bihar;Chhattisgarh;Goa;Gujarat;Haryana;HimachalPradesh;Jammuan&Kashmir;Jharkhand;Karnataka;Kerala;MadyaPradesh;Maharashtra;Manipur;Meghalaya;Mizoram;Nagala;Orissa;Punjab;Rajasthan;Sikkim;TamilNadu;Tripura;Uttaranchal;UttarPradesh;WestBengal".split(";");

var distinct = [];

$.each(data , function(i, el){
    if($.inArray(el, distinct ) === -1) 
        distinct.push(el);
});

//distinct will not contain any duplicates

Or if you dont want to use jquery (wont work in ie7):

var distinct = data.filter(function(elem, pos) {
    return data.indexOf(elem) == pos;
});

Fiddle

Johan
  • 35,120
  • 54
  • 178
  • 293
  • Its not working fine.Can u please modify it? once i enter the state name....it should not appear in the next attempt when we are entering another sate name but its not happening like that...Its again comming there in the list... – Raj Oct 26 '12 at 12:42
  • @Raj Have a look at the fiddle. I added a duplicate called "foo" in the beginning of the array. It does only appear once in the arrays. What part is not working for you? – Johan Oct 26 '12 at 12:46
  • @Raj Do you want to remove items from the array? Please elaborate – Johan Oct 26 '12 at 12:49
  • I have a textbox where i am using autocomplete property to that textbox. first time when i selecting any state like AndraPradesh, for the next time when we try to select from the list AndraPradesh should not be there(textbox contain multiple selection). but using ur code AndraPradesh there, which i dont want. So how to do it? – Raj Oct 26 '12 at 12:58
  • i think ur not getting wat i am saying..i will elaborate the problem...in text box when i type letter A...the states contain A letter will come as dropdown....and then if i selected Andhrapradesh..n again when i am trying to enter mutiple values like when again i type letter A ....Andhrapradesh should not come again in dropdown...in multiple value entry..no duplicates should not be enter...That var distint code is not working...if not wer to keep that code...Fiddle – Raj Oct 27 '12 at 05:34