0

I have a list of urls(div) underneath an input field(div). I need the ability to come down in the list and than by hitting enter this will trigger some function designated to the url. This is the fidle: the script

Over the last days I have tried to much things to explain, but in conclusion none of it worked. Help would be appreciated.

Youss
  • 4,196
  • 12
  • 55
  • 109
  • 1
    This link maybe can help you. (http://stackoverflow.com/questions/155188/trigger-button-click-with-javascript-on-enter-key-in-text-box) – 2GDev May 20 '11 at 15:44
  • Hi @2GDev The link you provided is all about static elements. I now how to approach this. The urls in the fiddle are dynamic, I want to be able to use my keybord (arrow up/down) to highlight the url(which I have done with css) and than by pressing enter some function would have to be triggered. – Youss May 20 '11 at 15:57
  • i've tried this : // our document is ready $(document).ready(function() { $("#searchbox").keyup(function(event){ if(event.keyCode == 13){ //here you have to go to link } }); With this you can handler the enter key but i've to find what function to call to display result... – 2GDev May 20 '11 at 16:06
  • I think getSearch(); but Im not sure since every function is entangled with other functions. – Youss May 20 '11 at 16:21

1 Answers1

1

After this line of code :

// set timers to do automatic hash checking and suggestions checking
setInterval(checkHash,500);
setInterval(checkSuggest,500);

Insert this :

$('#searchbox').keyup(
function (e){
    var curr = $('#suggest').find('.current');
    if (e.keyCode == 40) 
    {                                      
        if(curr.length)
        {
                $(curr).attr('class', 'display_box');
                $(curr).next().attr('class', 'display_box current');
        }
        else{
            $('#suggest li:first-child').attr('class', 'display_box current');
        }                    
    }
    if(e.keyCode==38)
    {                                        
        if(curr.length)
        {                            
                $(curr).attr('class', 'display_box');
                $(curr).prev().attr('class', 'display_box current');
        }
        else{
            $('#suggest li:last-child').attr('class', 'display_box current');
        }            
    }

    if(e.keyCode==13)
    {
        var search_terms = $('.current a').text();
        // perform a search with this text...
        doSearch(search_terms,true,false);
        //update the search textbox
        $('#searchbox').val(search_terms);
    }
})

    And don't forget to delete the previous code at the bottom...
2GDev
  • 2,478
  • 1
  • 20
  • 32
  • thanks for your effort. But what do you mean with the phrase: ''with the text searched'' ? – Youss May 20 '11 at 16:46
  • if you run the script when you press enter on a result you got the search, but the search textbox contains the old search... so for a complete functionality you have to update the textbox with the new search terms... – 2GDev May 20 '11 at 16:48
  • Please look at the fiddle: http://jsfiddle.net/yomoore/sjKgF/32/ Your code doesn't get the search after pressing enter...Maybe you misunderstood the question? Otherwise can you please be more clear? – Youss May 20 '11 at 16:58
  • you're right... but the problem is that the function is outside the document.ready and can't find the function doSearch... I've update my response to show you where you can put my code... – 2GDev May 20 '11 at 17:25
  • hahaha Im new at this so I forgive my self:) Thanks a lot for your time and effort, you have saved me a lot of headache. – Youss May 20 '11 at 17:38