jQuery - How can I open a URL in list using Enter key
<p>I have an jQuery "auto-suggest" type search box.  It populates the search results list with Href's so that users can then click on an item in the list to go to the relevant page.  Users can also scroll through the list using arrow keys to make a selection.  What I am trying to do now is allow users to hit the Enter key to go to their selected URL.  I have the Enter key working, but I'm not sure how I should construct the list or extract the Href from the list etc.. or should I have a second, hidden list which just contains the pure URL??</p>

<p>Here is the code so far with the relevant section marked ###:</p>

<pre><code>&lt;script&gt;

var results_list = '';

function callback(result) {
    results_list.show();
    var items = [];
    $.each(result, function (i, item) {
        items.push("&lt;li&gt;&lt;a href='/view/raw/" + item.collection + "/" + item.classname + "/" + item._id + "'&gt;" + item.Title + "&lt;/a&gt;&lt;/li&gt;");
    });
    results_list.append(items.join(''));
}

var list_selected;
var li = '';

$('.search').each(function () {
    $(this).keyup(function (e) {
        var search_type = $(this).attr('name');
        results_list = $('#' + search_type + '_list');
        li = results_list.children();
        var key_code = e.keyCode;
        if ($.inArray(key_code, [37, 38, 39, 40]) &gt; -1) {
            if (e.which === 40) {
                if (list_selected) {
                    list_selected.removeClass('selected');
                    next = list_selected.next();
                    if (next.length &gt; 0) {
                        list_selected = next.addClass('selected');
                    } else {
                        list_selected = li.eq(0).addClass('selected');
                    }
                } else {
                    list_selected = li.eq(0).addClass('selected');
                }
            } else if (e.which === 38) {
                if (list_selected) {
                    list_selected.removeClass('selected');
                    next = list_selected.prev();
                    if (next.length &gt; 0) {
                        list_selected = next.addClass('selected');
                    } else {
                        list_selected = li.last().addClass('selected');
                    }
                } else {
                    list_selected = li.last().addClass('selected');
                }
            }
        } else {
            // ### relevant section ###
            if (key_code == 13) {  // what to do here??
                window.location = list_selected.html();
            // ###
            } else {
                results_list.empty();
                var params = {
                    'search_type': search_type,
                        'q': $(this).val()
                };
                if ($(this).val()) {
                    ajaxThis("search_box", params, callback);
                }
            }
        }
    });
});

&lt;/script&gt;
</code></pre>
