user1592380
user1592380

Reputation: 36215

How to open a new window using a variable in jquery

I have a complex jquery expression which I have stored in the variable "that":

that = $('table[class=a]').find('a[href^="xx"]').closest('td')[0]


<td>
    <a href="xx....">
</td>

I'm not sure how to open the contained link using "that" as the starting point. I tried

 $(that>'a').each(function(){
            window.location.href = $(this).attr('href');
       });

this yields an empty set in firebug. How can I fix this?

Upvotes: 0

Views: 55

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173522

Instead of trying to find the first enclosing <td>, you can simply shorten the expression to get only the anchors:

$('table.a a[href^="xx"]').each(function() {
    location.href = this.href;
    return false;
});

This will take the first anchor, and if it exists, change the location.

Upvotes: 2

Related Questions