[jquery] Get href attribute on jQuery

I have some table rows

<tr class="b_row">
    <td>
        <div class="cpt">
            <h2>
                <a href="/ref/ref/1.html">example</a>
            </h2>
        </div>
    </td>
</tr>

<!--more elements -->

<tr class="b_row">
    <td>
        <div class="cpt">
            <h2>
                <a href="/ref/two/23.html">example N</a>
            </h2>
        </div>
    </td>
</tr>

I need to get hyperlinks in attribute. I use this script

function openAll()
{
    $("tr.b_row").each(function(){
    var a_href = $('div.cpt').find('h2 a').attr('href');
    alert ("Href is: " + a_href);
}

Problem: variable a_href is always / ref/ref/1.html

This question is related to jquery get href each attr

The answer is


Very simply, use this as the context: http://api.jquery.com/jQuery/#selector-context

var a_href = $('div.cpt', this).find('h2 a').attr('href');

Which says, find 'div.cpt' only inside this


add a reference to this, which refers to your b_row:

$("tr.b_row").each(function(){
    var a_href = $( this ).find('div.cpt h2 a').attr('href');
    alert ("Href is: "+a_href);
});

Use $(this) for get the desire element.

function openAll()
{
     $("tr.b_row").each(function(){
        var a_href = $(this).find('.cpt h2 a').attr('href');
        alert ("Href is: "+a_href);
     });
}

Use this:

$(function(){
    $("tr.b_row").each(function(){
    var a_href = $(this).find('div.cpt h2 a').attr('href');
    alert ("Href is: "+a_href);

    });
});

See a working demo: http://jsfiddle.net/usmanhalalit/4Ea4k/1/


var a_href = $('div.cpt').find('h2 a').attr('href');

should be

var a_href = $(this).find('div.cpt').find('h2 a').attr('href');

In the first line, your query searches the entire document. In the second, the query starts from your tr element and only gets the element underneath it. (You can combine the finds if you like, I left them separate to illustrate the point.)


Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to get

Getting "TypeError: failed to fetch" when the request hasn't actually failed java, get set methods For Restful API, can GET method use json data? Swift GET request with parameters Sending a JSON to server and retrieving a JSON in return, without JQuery Retrofit and GET using parameters Correct way to pass multiple values for same parameter name in GET request How to download HTTP directory with all files and sub-directories as they appear on the online files/folders list? Curl and PHP - how can I pass a json through curl by PUT,POST,GET Making href (anchor tag) request POST instead of GET?

Examples related to href

File URL "Not allowed to load local resource" in the Internet Browser Angular 2 router no base href set How to redirect to a route in laravel 5 by using href tag if I'm not using blade or any template? Html- how to disable <a href>? Add php variable inside echo statement as href link address? ASP MVC href to a controller/view href="tel:" and mobile numbers How can I disable the bootstrap hover color for links? How can I disable HREF if onclick is executed? Open link in new tab or window

Examples related to each

jQuery animated number counter from zero to value How to iterate over array of objects in Handlebars? Excel VBA For Each Worksheet Loop jQuery looping .each() JSON key/value not working jQuery '.each' and attaching '.click' event How to use continue in jQuery each() loop? jQuery .each() with input elements C++ for each, pulling from vector elements jQuery each loop in table row Get href attribute on jQuery

Examples related to attr

Error:(9, 5) error: resource android:attr/dialogCornerRadius not found jQuery get the name of a select option Set attribute without value Setting new value for an attribute using jQuery jQuery check if attr = value Get href attribute on jQuery JQuery - File attributes jquery: get value of custom attribute How to find elements with 'value=x'? jQuery .attr("disabled", "disabled") not working in Chrome