Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get a href value from a link inside li list?

Tags:

jquery

Here is a Fiddle

I want to get the href attribute, I use $(this).attr('href') but it does not work!

HTML :

<div class="wrap_atletas_interno">
    <ul>
        <li class="atleta">
            <a href="teste.html">
                <div class="nome_86_atleta">Antônio</div>
                <img src="atletas/antonio_86px.jpg" />
            </a>
        </li>
        <li  class="atleta">
            <div class="nome_86_atleta">Cauê</div>
            <img src="atletas/caue_86px.jpg" />
        </li>
        <li class="atleta">
            <div class="nome_86_atleta">Dudu</div>
            <img src="atletas/dudu_86px.jpg" />
        </li>
    </ul>
</div>

JavaScript :

$('.atleta').click(function (e) {
    e.preventDefault();
    $('.atleta').removeClass('atleta_atual');
    $(this).addClass('atleta_atual');
    var h = $(this).attr('href');
    alert(h);
    $.get(h, function (data) {
        //$(".detalhes_atleta").html(data).fadeIn("slow");
        alert(h);
    });
});
like image 662
Preston Avatar asked Jan 05 '13 19:01

Preston


People also ask

What does href =# do?

Definition and Usage The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!

How do you assign a value to a hyperlink?

Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

Is href a property or attribute?

The HTML <a> href Attribute is used to specify the URL of the page that the link goes to. When the href attribute is not present in the <a> an element that it will not be a hyperlink. This attribute is used to specify a link to any address.


2 Answers

Check the updated fiddle

Changed

var h = $("a",this).attr('href');
like image 149
Akhil Thayyil Avatar answered Oct 24 '22 21:10

Akhil Thayyil


$(this) is referring to the parent li of the link. You need to use

$(this).find('a').attr('href');

Also please fix your html, block elements should not be inside inline elements.

In your case the div should not be in the a

like image 28
Blake Plumb Avatar answered Oct 24 '22 21:10

Blake Plumb