JavaScript - Autocomplete Search Not Opening Link
Hi there
Im a relative newbie and am trying to have a searchbar that users can complete, with the text entry being from a pre-defined list. The hope is that a new page will open with the link that relates to the search bar entry. Im using an Autocomplete code but cant work out how to get the link part to work. See below code Code: <html> <head> <script src="jquery-latest.js"></script> <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/demo/main.css" type="text/css" /> <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type="text/css" /> <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js"></script> <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.dimensions.js"></script> <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"></script> <script> $(document).ready(function(){ var data = "092 610 343 505 Utilities".split(" "); $("#example").autocomplete(data); }); var data = [ {text:'Link A', url:'/page2'}, {text:'Link B', url: '/page2'} ]; $("...").autocomplete(data, { formatItem: function(item) { return item.text; } }).result(function(event, item) { location.href = item.url; }); </script> </head> <body> Number: <input id="example" /><p></p> </body> </html> Can someone help me complete this code? For example, how can I change the code so that a text entry of "092" opens "www.google.com" when I hit enter after text entry? Your help with this is greatly appreciated Glen Similar TutorialsHi there, Ive been asked to find out something in work and I hope someone can help me with this. I am looking to put a search box within a website. Users in work will be searching by 1 of 50 keywords, and each of these key words relates to a different web page that I want the search to open. For Example, typing 'one' in search bar opens link 'one.htm', typing 'two' in search bar opens link 'two.htm' and so on. Ideally it would be great if the seach bar will autocomplete text as well, but anything will be of great help to me!! Is it possible to get a code to do this? As Im a relative newbie, any help you can provide will be greatly appreciated. Many Thanks Glen Hi All, Im a newbie when it comes to Javascript and trying to learn as I go along. I am working on an internal work site. In the site I have a text box and on entering a specific code I want it to open a new page that links to a document that has the same name. ie. if user types red, I want it to find the document red.html in a folder. To make my colleagues job easier, I want the text box to autocomplete. Below is a section of the code I plan to use Code: <script> var arrValues = ["red", "orange", "yellow", "green", "blue", "indigo", "violet", "brown"]; </script> </head> <body> <h2>Autocomplete Textbox Example</h2> <p>Type in a color in lowercase:<br /> <input type="text" value="" id="txt1" onkeypress="return autocomplete(this, event, arrValues)" /></p> </body> </html>Can anyone help me to modify this to: 1) link the text entered to the document I want opened 2) open this document upon 'enter' pressed If anyone can provide help it will be greatly appreciated. Thanks Glen ok so here what i want to do (im a noob): i want a dropdown menu i want a button (go) i want ten different items in the dropdown menu if i choose the first item and click go i want a new page to open in the same window if i choose the second item and click go i want to a different page to open in the same window and so on all ten items lead to a different link when i click go....how do i do this?? thanks!! I am new to javascript and am trying to make a script that would open a link every xx randomly seconds. So far I have come up with this after searching: Code: <script> function reloadMe() { window.location.reload(); } function getInterval(){ var lowerBound = 30; var upperBound = 60; var randNum = Math.floor((upperBound-lowerBound+1)*Math.random()+lowerBound) * 1000; return randNum; } var interval = getInterval(); var srcInterval = setInterval("reloadMe()",interval); </script> This works on refreashing a page every xx seconds between 30 and 60 seconds. But it does not open a link. Is it possible to make a script that opens a link every xx seconds at random. It would be even greater if it can exclude certian times of the day, or can that be done with a cron job? Hi guys, i've been the last few days around a script that i can't make it work, i'm a beginner in javascript so if you guys could help me i'd appreciated. This script is a search engine, when you look for something say the word "titanic" , he shows you a list of results with numbers, being the first link with the higher number (kinda like a score), and then after u click the link you want he goes to the respective page. Now i want him to skip the results page, and directly open the first link that he founds on the page. The Script __________________________ <!-- Begin var item = new Array(); // Just enter as many additional pages that you want to search, then fill in he // additional listings for each page. // "Page Name","path","Page Title","Many,Key,Words","Descriptive Comments" c=0; item[c]=new Array("centerif.htm","","titanic","index,main,start,home,front","Demonstration search engine data about an imagined but probable internet site."); c++; item[c]=new Array("indexg.htm","","About Me","about,author,contact,email,who","Contact details and general information about the creator of the site and what the site is about."); c++; item[c]=new Array("links.htm","","Links page","links,more,where,similar,friends","Links to my favourite sites which I find interesting. Other friends sites which have similar interests to my own."); c++; item[c]=new Array("main.htm","main/","Main Page","content,main,focus","The main part of my site which contains what you have come to see. Lots of stuff like that and more great things. All in a sub directory."); c++; item[c]=new Array("logo.jpg","main/images/","Link Logo","link,image,logo,graphic","The logo.jpg is just a small image which you can place on your site as a link to me. It's in a second level subdirectory."); page="<html><head><title>Search Results</title></head><body bgcolor='white'><center><table border=0 cellspacing=10 width=80%>"; function search(frm) { win = window.open("","","scrollbars"); win.document.write(page); txt = frm.srchval.value.split(" "); fnd = new Array(); total=0; for (i = 0; i < item.length; i++) { fnd[i] = 0; order = new Array(0, 4, 2, 3); for (j = 0; j < order.length; j++) for (k = 0; k < txt.length; k++) if (item[i][order[j]].toLowerCase().indexOf(txt[k]) > -1 && txt[k] != "") fnd[i] += (j+1); } for (i = 0; i < fnd.length; i++) { n = 0; w = -1; for (j = 0;j < fnd.length; j++) if (fnd[j] > n) { n = fnd[j]; w = j; }; if (w > -1) total += show(w, win, n); fnd[w] = 0; } win.document.write("</table><br>Total found: "+total+"<br></body></html>"); win.document.close(); } function show(which,wind,num) { link = item[which][1] + item[which][0]; line = "<tr><td><a href='"+link+"'>"+item[which][2]+"</a> Sco "+num+"<br>"; line += item[which][4] + "<br>"+link+"</td></tr>"; wind.document.write(line); return 1; } // End --> </script> __________________________________________ Once again thanks guys, it's appreciated !! Hi all!, I'm having a few challenges trying to create a link to a "new" search from a database search results page. This maybe a better example: My goal is: To allow website visitors to searches site for i.e. "Dog Collars", the results page lists every "dog collar" and store that carries "dog collars" (in database). At that point I would like to have links (on the results page) to all brands of "dog collars" and stores that carry "dog collars". I would like to then be able to click on a "brand" (text link) of "dog collars" and then generate a new results page that lists all store and vendor information. thank you in advance! Erick p.s. this is a link to a site that has a similar featu http://69.0.211.58/~pubexpress/ami/html/index.html Ok so im new to javascript and I have a button in an interface for an app. It's like drag and drop and i want this button to link you to a certain website searching for what the user types in the searchField1 box. The button is button16. Such as you type dogs into the text box and you click the google button among other search engines and it will redirect you to a google search doing a search for dogs. But how do I make this button search google and search for whats in searchField1. I would really appreciate the help. Im looking everywhere to learn this but just cant find it. If you could tell me how or send me a link to a tutorial that would be great. Thanks! Here is the code: I don't know where and what to add to do that. Please help. Hi, I'm really new to Javascript. Recently in my IT class, we made a HTML page which would open a popup box, where the user could enter a key word. When the user pressed enter, the page would navigate to a specific page. The code we used was: Code: <script language = "JavaScript"> where = window.prompt ("Please tell me where you would like to go."); switch (where){ case "Digg" : window.location = "http://www.digg.com" ; break; default: window.location = "http://www.google.com" ; } </script> What I am hopeing to do, is implement this code on my workplaces server, and have the keywords link to other html documents within the server. However when I tested this, for some reason the links are not working. Can this actually be done? Am I missing something silly? Are there any other ways of doing this? Thanks in advance So I found this code and I've manipulated it a little bit for what I want. It was designed to autocomplete an input field when countries were typed, but I changed the values to match ingredients or cooking terms, etc. I was wondering if it would be possible through javascript to autcomplete a word after one is already typed in the input field. For instance, if one person types an 'f' into the input field it will suggest 'flour' as an autocomplete, but if the person types 'f' again, it will not perform the same autocomplete function. How would you go about doing that? Thank you! Code: <head> <script type="text/javascript"> /* This script and many more are available free online at The JavaScript Source!! http://javascript.internet.com Created by: Timothy Groves | http://www.brandspankingnew.net/ */ countries = new Array( "flour", "cup", "tsp.", "tbsp.", "lb.", "pound", "tablespoon", "teaspoon", "sugar", "olive oil", "vegetable oil", "gallon", "quart", "qt." ); var sug = ""; var sug_disp = ""; function getCountry() { var input = document.forms['address_frm'].country.value; var len = input.length; sug_disp = ""; sug = ""; if (input.length) { // get matching country from array for (ele in countries) { if (countries[ele].substr(0,len).toLowerCase() == input.toLowerCase()) { sug_disp = input + countries[ele].substr(len); sug = countries[ele]; break; } } } document.forms['address_frm'].sug_country.value = sug_disp; if (!sug.length || input == sug_disp) document.getElementById('sug_btn').style.display = "none"; else document.getElementById('sug_btn').style.display = "block"; } function setCountry() { document.forms['address_frm'].country.value = sug; hideSug(); } function hideSug() { document.forms['address_frm'].sug_country.value = ""; document.getElementById('sug_btn').style.display = "none"; } </script> </head> <body> <div style="width: 202px; margin: 100px auto 0 auto;"> <form name="address_frm"> <div></a> <div style="position: relative; margin: 5px 0 5px 0; height: 30px;"> <div style="position: absolute; top: 0; left: 0; width: 200px; z-index: 1;"> <input type="text" name="sug_country" style="background-color: #fff; border: 1px solid #999; width: 200px; padding: 2px" disabled /> </div> <div style="position: absolute; top: 0; left: 0; width: 200px; z-index: 2;"> <input autocomplete="off" type="text" name="country" style="background: none; color:#000; border: 1px solid #999; width: 200px; padding: 2px" onfocus="getCountry()" onkeyup="getCountry()" /> </div> <div id="sug_btn" style="position: absolute; top: 2px; right: 5px; z-index: 4; display:none;"> <p onclick="setCountry()" style="cursor:pointer">></p> </div> </div> </form> </div> </div> </body> How do I fix the problem that autocomplete does not validate, I have heard before that there is a dirty way to do this but did not want to do this before, but now feel i must as my boss wants the site error free ! does anyone know how I do this ? Hi all, I am trying to create a dependend autocomplete (based on JQuery) box for example: Field 1 has a autocomplete value of America. Field 2 has to fetch based on the value America the availible states like Kansas However if field 1 has the value England i want field 2 to reflect this by offering for example Schotland. I have the following code in place: Code: $( document).ready( function() { var ac_config = { source: "getcountry.php", select: function(event, ui){ $("# field1").val(ui.item.name), $("# field1hidden").val(ui.item.id); }, minLength:3 }; $("# field1").autocomplete(ac_config); } ); The code above will autocomplete the field1 (country's) input field and an hidden id field. Now i want to base the selection of field2 (the states) on the returned value in field1. i have the following code to try and do this (placed inside document.ready function()): Code: var stateinput = document.getElementById('field1').value var ac_config2 = { source: "getstate.php?country=" + stateinput, select: function(event, ui){ $("# field2").val(ui.item.name), $("# field2hidden").val(ui.item.id); }, minLength:3 }; $("# field2").autocomplete(ac_config); However the selection is not restricted based on the given input. Can anybody tell me what i am doing wrong? Thanx in advance..... Hi So, I have a working single autocomplete function, but I want to use the same function on different fields.... this is my code: Code: <script type="text/javascript"> function lookup(inputString) { if(inputString.length == 0) { // Hide the suggestion box. $('#suggestions').hide(); } else { $.post("rpc.php", {queryString: ""+inputString+""}, function(data){ if(data.length >0) { $('#suggestions').show(); $('#autoSuggestionsList').html(data); } }); } } // lookup function fill(thisValue) { $('#inputString').val(thisValue); setTimeout("$('#suggestions').hide();", 200); } </script> <div> <form> <script type="text/javascript"> var index = '1'; </script> <div> Type your county: <br /> <input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" /> </div> <div> Type your county: <br /> <input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" /> </div> <div class="suggestionsBox" id="suggestions" style="display: none;"> <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" /> <div class="suggestionList" id="autoSuggestionsList"> </div> </div> </form> </div> I know the id's are the same in this, but I tried changing them and it still seems to make little difference.. I did wonder if I could add something variable to the id's and pass that in the function call, but could not figure it out.... the above gives me autocomplete on two fields, but will only fill one of them... Any suggestions would be appreciated Hello, i have coded this page: http://www.advance2000.net/system/ad...rchasepeso.php click on the "ADD ITEM" button TWICE. now you have created two rows of textboxes. next, enter the letter "M" on the description textbox on the second row. this would then initialize an autocomplete function... you will see two words "memory" and "motherboard" try clicking one of these.. after clicking, i guess you will notice the error in my program. the clicked value (for example "memory") would be sent in the FIRST ROW TEXTBOX instead of the second.... can anyone help me? Here is the Code of the said Dynamic Textbox: Code: <script language ="javascript"> row_no=0; function addRow(tbl,row){ row_no++; if (row_no<=200){ if (row_no<=200){ if (row_no>=100){ var textbox = '<input type="text" id="item[]" value='+row_no+' size = "2" maxlength= "99" name="item[]>"';} if (row_no<100){ var textbox = '<input type="text" id="item[]" value='+row_no+' size = "2" maxlength= "99" name="item[]">';} var textbox2 = '<input type="text" id="stockcode[]" size = "20" maxlength= "100" name="stockcode[]">'; var textbox3 = '<input type="text" autocomplete="off" id="description" size = "95" maxlength= "100" name="description" onkeyup="lookup(this.value);" onChange="fill();"><div class="suggestionsBox" id="suggestions" style="display: none;"><img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" /><div class="suggestionList" id="autoSuggestionsList"></div>'; var textbox4 = '<input type="text" id="qty'+row_no+'" size = "6" maxlength= "100" onChange="Sum()" name="qty[]">'; var textbox5 = '<input type="text" id="price'+row_no+'" size = "15" maxlength= "100" onChange="Sum()" name="price[]">'; var textbox6 = '<input type="text" id="subtotal'+row_no+'" size = "15" maxlength= "101" onChange="Grand()" name="subtotal[]" readonly>'; var tbl = document.getElementById(tbl); var rowIndex = document.getElementById(row).value; var newRow = tbl.insertRow(row_no); var newCell = newRow.insertCell(0); newCell.innerHTML = textbox; var newCell = newRow.insertCell(1); newCell.innerHTML = textbox2; var newCell = newRow.insertCell(2); newCell.innerHTML = textbox3; var newCell = newRow.insertCell(3); newCell.innerHTML = textbox4; var newCell = newRow.insertCell(4); newCell.innerHTML = textbox5; var newCell = newRow.insertCell(5); newCell.innerHTML = textbox6; } if (row_no>200){ alert ("Too Many Items. Limit of 20."); } } } </script> The VAR TEXTBOX 3 would create the said "description textbox" that is also the textbox that calls the "Fill()" and the Lookup() function of the autocomplete HERE, we have the said functions: Code: function lookup(description) { if(description.length == 0) { // Hide the suggestion box. $('#suggestions').hide(); } else { $.post("rpc.php", {queryString: ""+description+""}, function(data){ if(data.length >0) { $('#suggestions').show(); $('#autoSuggestionsList').html(data); } }); } } // lookup function fill(thisValue) { $('#description').val(thisValue); setTimeout("$('#suggestions').hide();", 200); } Hello, I have a problem with autocomplete of jQuery. Here is my code Code: $('input#recipient').autocomplete ({ source: function (request, callback) { var dataString = {username : request.username}; $.ajax({ url: url.root + 'email/', data: dataString, //cache: false, complete: function(xhr, result) { if(result != 'success') return; var response = xhr.responseText; var usernameList = []; $(response).filter('li').each (function() { usernameList.push($(this).text()); )}; callback(usernameList); } }); } }); Can you tell me what's the problem Thank you I want to use a database query to grab all of a member's friends from a table in order to add the friend's email address to a form, to ready it for POSTing. I am using the jquery ui, so on my form.php page, I have the script and html: Code: <script> $(function() { $( "#friendsearch" ).autocomplete({ source: "search.php", minLength: 2, select: function( event, ui ) { log( ui.item ? "Selected: " + ui.item.id + " " + ui.item.value : "Nothing selected, input was " + this.value ); } }); }); </script> <form>... <input id="friendsearch" /> </form> And on my search.php I have: Code: //code to connect to database.... etc $query = " //this code goes through a table LEFT JOIN to grab a list of the logged-in member's friends' info (id, email, first name, last name) " $sql = mysql_query($query); while($row = mysql_fetch_array($sql)) { $fid = $row["id"]; $firstname = $row["firstname"]; $lastname = $row["lastname"]; $email = $row["email"]; echo $fid <br /> $f_firstname <br /> $f_lastname <br /> $email <br /> "; The "echo" above works. It lists out all the info perfectly, so I know my query is good. The problem, I am having, is with what comes next in the search.php: Code: $q = strtolower($_GET["term"]); // supposed to do something with $q here (not sure what!) $return = array(); while ($row = mysql_fetch_array($query)) { array_push($return,array('id'=>$row['firstname'],'value'=>$row['lastname'])); } echo(json_encode($return)); This is the part that's suppose to work with the jquery autocomplete. I know it's wrong, but I need to know how I can fix it to do the following: 1) when the user starts typing the first name or last name of the friend, auto-complete shows a drop-down list of firstname lastname. 2) once the friends' names are placed in the input box, on submit, i want to POST the friends' email addresses (not their name)! Complex, I know... Any help would be much appreciated! Thanks, June http://auspost.com.au/apps/postcode.html Basically I want to build the same thing. The data is freely available there as a download. I couldn't find anything existing in the jquery plugin space. Can anyone offer pointers on how to achieve it? I have a JQuery autosuggest working properly on my form.php page... Now I want to get my form to actual POST the input values. So currently if I just run search.php, I get the following (using echo json_encode): {"label":"Henry Gale","value":"henrygale@gmail.com"},{"label":"Amy Gerges","value":"amy@yahoo.com"}, and the list goes on. So since the search is working properly. Now, I want to POST only the values that I place in the form's input field. I currently have: Code: <script> $(document).ready(function() { $( "#autocomp" ).autoSuggest("search.php", { minChars: 2, selectedItemProp: "label", searchObjProps: "label", selectedValuesProp: "value", selectionLimit: 8, formatList: function(data, elem){ var my_image = data.image ; var my_label = data.label; var new_elem = elem.html(my_image + my_label); return new_elem; } }); }); </script> <input name="contacts" type="text" id="autocomp" maxlength="35" /> But if I do an echo of the $_POST['contacts'] I just get the word: Array I am doing something wrong, just not sure what... Since my input gets a list of comma separated values, how can I: 1) make sure the input values get the "value" attribute which corresponds to the emails. 2) post the emails so I can do things with the emails (run them through format checks, insert them into a mysql db, etc). I would like to use a JavaScript to fill in the total based on the responses given by the user in this form stub:
Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Formstub</title> </head> <body> <form id="theform" id="theform" action="formstub.html" method="post"> <table width="90%" border = "1 " style="margin-left:40px; font-family: helvetica, ariel, sans-serif; font-size: 0.95em; color: #2D8AB9;"> <tr style="height:3em"> <td width="2em"><input type="checkbox" name="skipperReg" id="skipperReg" value="30" /></td> <td width="12em">Registration Fee</td> <td width="4em" >$<input type="text" name="skipperFee" id="skipperFee" readonly="readonly" size ="2" value="30" style="text-align:right" /></td> <td width ="20em"> </td> </tr> <tr> <td ><input type="checkbox" name="guestReg" id="guestReg" value="8" /></td> <td>Guest Fee</td><td>$<input type="text" name="guestFee" id="guestFee" readonly="readonly" size ="2" value="8" style="text-align:right" /></td> <td> # Guests <input type="text" name="guestQty" id="guestQty" size="2" /></td> </tr> <tr> <td ><input type="checkbox" value="" name="miscAmt" id="miscAmt" /></td> <td>Misc</td><td>$<input type="text" name="miscTotal" size ="2" style="text-align:right" /></td> <td> List details <input type="text" name="miscDetails" size="35" /></td> </tr> <tr style="height:2em"><td > </td> <td>Total</td><td>$<input type="text" id="total" name="total" size ="2" style="text-align:right" ></td> </tr> </table> </form> </body> </html> The script should add the skipperfee to the total if the skipperReg box is checked, multiply the guestFee by the guestQty and add that to the total if the guestReg box is checked, and add the miscTotal to the total if the miscAmt box is checked. Here is what I'm envisioning: The user selects a state, then in the 'Cities' box, he starts typing the name of his city. However, it autocompletes the name for him based on the state he chose. So if he chose California and started typing in "Los Angeles", it would autocomplete "Los Angeles" for him. Does that make sense? Is there a script out there that can do this? i have this, Code: $(function() { $('#input').autocomplete('test.php', {json: true}); }); that bring back values from the database here is my php section Code: $result = mysqli_query($db,$query); while( $row = mysqli_fetch_array($result) ) { $aResults[] = array( "id"=>($row['s_id']) ,"value"=>($row['s_last']), "info"=>($row['s_first']) ); } $return = array(); foreach ($aResults as $key => $res) { $return[] =$res['value'] . ' , ' . $res['info']; } echo json_encode($return); } } } i would like, when i select a name from the auto suggest to be able to have the id be filled in a input text area... uncle Google says this, but i am having a hard time implementing it. Code: myCallback = function(li, $input) { // I need to refer to the appropriate "myXxxInput" here alert($input.attr('id')); } setup = function() { setupInput($('#myFirstInput')); setupInput($('#mySecondInput')); } function setupInput($input) { $input.autocomplete('blah.php', {onItemSelect: function(li) { myCallback(li, $input);} }); } |