You need to solve in reverse order. Ie to take all referring to within the specified div'a and to filter the href so that the current reference is to the end of the document.URL, even here the jQuery is not needed
\r
var links = document.getElementById('myDiv').getElementsByTagName('a'), result = [], $result; for(var i = links.length - 1, j = 0; i > -1; i--) { if( document.URL.indexOf(links[i].href) + links[i].href.length != document.URL.length ) { continue; } result[j++] = links[i]; $result = $result.jquery ? $result.add(links[i]) : $(links[i]);//the result as a jquery object }
\r
But there is a small "but", e.g. "mysite/" and "mysite/index.html" point to the same page, with all the attendant nuances