On Sep 7, 2007, at 18:33 , ScottBruin wrote:

In this search, pressing enter when an item is highlighted redirects
to a new page. The code from the book reads similar to the following:
        var redirectPage = function() {
                var x = $autocomplete.find('li').eq(selectedItem).children()[0];
                location.href = x;
        };

where, from earlier:

        var $autocomplete = $('<ol id="dropbox"></ol>').hide().insertAfter($
('<some search field>'));


"selectedItem" is the currently highlighted list element (<li>) in the
drop down div. Its my understanding, then, that the redirectPage
function should find the list item that is currently highlighted, and
then find its first child. This first child is a link containing text
(a la <a href="google.com">Google</a>).

To me, it seems that .children()[0] should return the entire string:
"<a href="google.com">Google</a>"

However, it only returns "google.com".

It should be returning neither. Using the [0] notation on a jQuery object (which is what the .children() method returns) gives you a DOM node object. It may be that whatever diagnostic technique you are using to inspect the return value is using the href as a label for the object.


Further, when I change var x =
$autocomplete.find('li').eq(selectedItem).children().html() it does
return the HTML inside the anchor, namely "Google".

In this case, you are in effect calling innerHtml on each of the item's children, and concatenating the results. Because only the string "Google" is in the anchor, this is what is returned.

--
Jonathan Chaffer
Technology Officer, Structure Interactive


Reply via email to