Jquery UI autocomplete help

I am trying to implement autocomplete 6 times on my page, and I can do it once, but was wondering if there was a way to shorten the code somehow without copying the whole script 6 times.

My code is below... is there a way to just copy the the $( "#titles2" ).autocomplete part over and over so that it can use the same code that gets it from the xml file?
<script>
	$(function() {
	

		$.ajax({
			url: "/scripts/jqueryui/dev/demos/autocomplete/titles.xml",
			dataType: "xml",
			success: function( xmlResponse ) {
				var data = $( "titlename", xmlResponse ).map(function() {
					return {
						value: $( "title", this ).text(),
						spanishvalue: ( $.trim( $( "titlespanish", this ).text() ) || "(no translation)" ),
						id: $( "titlenameId", this ).text()
					};
				}).get();				
				
				$( "#titles2" ).autocomplete({
					source: data,
					minLength: 0,
					select: function( event, ui ){
						
				$("#ListingTitleSpanish").val(ui.item.spanishvalue);
}				

				});

			}
		});
	});
	</script>

<div style="margin-top:0.5em; font-family:Arial">
<label for="titles2"><b>Choose a title:</b> </label>
<input id="titles2" size="70" class="ui-widget" onClick="this.select()" />
</div>

Open in new window

monosythAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NeuropsykopatCommented:
you mean data will be the same ?


<script>
      $(function() {
      

            $.ajax({
                  url: "/scripts/jqueryui/dev/demos/autocomplete/titles.xml",
                  dataType: "xml",
                  success: function( xmlResponse ) {
                        var data = $( "titlename", xmlResponse ).map(function() {
                              return {
                                    value: $( "title", this ).text(),
                                    spanishvalue: ( $.trim( $( "titlespanish", this ).text() ) || "(no translation)" ),
                                    id: $( "titlenameId", this ).text()
                              };
                        }).get();                        
                        
                        $( "#titles2" ).autocomplete({
                              source: data,
                              minLength: 0,
                              select: function( event, ui ){
                                    
                        $("#ListingTitleSpanish").val(ui.item.spanishvalue);
}                        

                        });
                        $( "#titles3" ).autocomplete({
                              source: data,
                              minLength: 0,
                              select: function( event, ui ){
                                    
                        $("#ListingTitleSpanish").val(ui.item.spanishvalue);
}                        

                        });
                        $( "#titles4" ).autocomplete({
                              source: data,
                              minLength: 0,
                              select: function( event, ui ){
                                    
                        $("#ListingTitleSpanish").val(ui.item.spanishvalue);
}                        

                        });
                  }
            });
      });
      </script>

<div style="margin-top:0.5em; font-family:Arial">
<label for="titles2"><b>Choose a title:</b> </label>
<input id="titles2" size="70" class="ui-widget" onClick="this.select()" />
<input id="titles3" size="70" class="ui-widget" onClick="this.select()" />
<input id="titles4" size="70" class="ui-widget" onClick="this.select()" />
</div>

if you want different data, you will have to duplicate data function

      
                        var data = $( "titlename", xmlResponse ).map(function() {
                              return {
                                    value: $( "title", this ).text(),
                                    spanishvalue: ( $.trim( $( "titlespanish", this ).text() ) || "(no translation)" ),
                                    id: $( "titlenameId", this ).text()
                              };
                        }).get();            

                        var data2 = $( "titlename", xmlResponse ).map(function() {
                              return {
                                    value: $( "title", this ).text(),
                                    spanishvalue: ( $.trim( $( "titleenglish", this ).text() ) || "(no translation)" ),
                                    id: $( "titlenameId", this ).text()
                              };
                        }).get();      
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
monosythAuthor Commented:
Thanks! I just don't know how to write jquery/js. I'm great at copying and pasting and making it work and it worked just how i needed it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.