Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: How do I get the url of an anchor contained within a clicked 'li' tag?

Tags:

jquery

The following is a segment of HTML in one of my pages:

<li class="prodcat-line">
   <a title="foobar" class="prodcat" href="/some/url.php">Foobar</a>
</li>

I want to be able to retrieve the url of the clicked on li tag. My "jQuery fu" IS NOT WHAT IT SHOULD BE. I know how to bind the click event of li elements of class "prodcat-line", but I don't know how to extract nested tags from within the clicked item - can anyone help?

like image 343
oompahloompah Avatar asked Apr 11 '11 07:04

oompahloompah


People also ask

How do I know which anchor is clicked in jQuery?

click(function(e){ var id = e.target.id; alert(id); }); }); In this way, e. target is the element you have clicked on.

How can I get the text value of a clicked link?

$('#my-div a'). click(function() { var txt = $(this). text(); console. log(txt); });

What attribute belongs to the anchor tag?

The <a> HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address.

What is an anchor tag link?

The <a> tag (anchor tag) in HTML is used to create a hyperlink on the webpage. This hyperlink is used to link the webpage to other web pages or some section of the same web page. It's either used to provide an absolute reference or a relative reference as its “href” value. Syntax: <a href = "link"> Link Name </a>


1 Answers

$('.prodcat-line').click(function(){
    alert($('a', this).attr('href'));
    return false;
});

Example here.

like image 192
Arthur Halma Avatar answered Nov 14 '22 12:11

Arthur Halma