• TechQA.

        refer to first link of particular div

        6.4k views Asked by Jamol At 2012-08-07T08:03:15+00:00 07 August 2012 at 08:03 2025-12-21T13:15:07+00:00

        I have following elements:

        <div class="content" id="content_1" style="display: none; ">
        
        <ul class="ad-thumb-list" style="width: 473px; ">
        
        <li> 
        <a href="../images/portfolio/portfolio_1_1.png" class="ad-thumb0 ad-active">
        <img src="../images/portfolio/thumbs/thumb_1_1.png"  style="opacity: 1; ">
        </a>
        </li>
        
            <li> 
        <a href="../images/portfolio/portfolio_1_2.png" class="ad-thumb0">
        <img src="../images/portfolio/thumbs/thumb_1_1.png"  style="opacity: 1; ">
        </a>
        </li>
        
          <li> 
        <a href="../images/portfolio/portfolio_1_3.png" class="ad-thumb0">
        <img src="../images/portfolio/thumbs/thumb_1_1.png"  style="opacity: 1; ">
        </a>
          </li>         
        
         </ul>
        
        </div>
        

        I need to get href attribute of first link <a> inside <ul> <li>

        I have several this kind of divs, as you can see it has id='content_1', so others will have id='content_2'... and so on.

        How can I get href of first a in first li of a called div with jQuery?

        jquery attributes refer
        Original Q&A
        2

        There are 2 answers

        1
        Arsen K. Arsen K. On 2012-08-07T08:55:22+00:00 07 August 2012 at 08:55 BEST ANSWER
        $('#content_1 li:first a').attr('href');
        

        if you have many divs, then:

        $('.content').each( function() {
            $(this).find('li:first a').attr('href');
        });
        
        2
        Chris Chris On 2012-08-07T08:12:24+00:00 07 August 2012 at 08:12

        The XPath to the a of interest would be:

        //div[@id="content_1"]/ul/li[1]/a
        

        Which means, anywhere below the root of the document find a div with the specified id. Under that, find a ul and get its first li. Inside that, there is an a.

        How you actually achieve that in your case depends on what you're working with (client-side JS? one of the frameworks? web-scraping script?).

        Related Questions in JQUERY

        • In Datatables, start value resets to 0, when column sorting
        • Bootstrap modal not showing at the desired position on a web page when the screen size is smaller
        • window.location.href redirects but is causing problems on the webpage
        • Using JQuery Date Slider
        • Storing selected language in localStorage
        • How to stop other divs from still showing when i click a different button?
        • Check multiple values with jQuery
        • Bootstrap component does not want to render in Datatables function
        • put white spaces when entering an amount moneytype symfony
        • Trouble accessing custom header in AJAX response using jQuery in Fiware Keyrock
        • I just cant make it work, HTML, JS and Firebase error
        • Didn't declared variable still not getting any error in JavaScript
        • Move element horizontally while scrolling vertically in pure JavaScript
        • allow multi carousel in same page
        • Embedded TikTok posts / thumbnail styling issue

        Related Questions in ATTRIBUTES

        • PyQgis: Attribute field does not get added
        • Getting data attributes from HTML input element with a datalist
        • Blender to Unity Vertex Color (Alpha) Data Doesn't Work
        • Do I need two attribute classes
        • Add custom attributes on terms.ftl page for Keycloak
        • How to disable HTML attributes with js?
        • How can I use has_shortcode() including the attribute value?
        • Regex to Find and Remove HTML ID Attribute That Starts With or Contains Specific Word
        • how to get html attribute using flask?
        • When is it safe to use [[no_unique_address]] in C++?
        • DXL : Comparing attributes of objects across modules
        • How to change connected class objects
        • Usage of __attribute__((aligned(4), packed)) with structures in C
        • Ambiguous error message in Firefox about partitioned attribute in cookie
        • Woocommerce product option - Wordpress Post titles as a dropdown menu

        Related Questions in REFER

        • Redirect Page If Not Referred
        • How to change document.referrer when having server side redirect?
        • how to make auto-scroll after a click in react
        • First xslt extract one element from xml and apply a root node, second one simply removes namespaces and attributes. Needs to combine both into one
        • How to refer to a custom slider inside a listview in Xamarin Forms
        • Page stucked when href refering to a hash
        • Python - How to avoid refer multiple names to one dict
        • Two dataframes connected by index
        • is there any method or api available to create unique referral link for telegram bot with python
        • How can I hide from SimilarWeb websites that refer to my websites?
        • What decides when the git reference-transaction hook runs?
        • How to refer from a column to names of other columns and create a new column
        • Using a value stored in a vector to delete rows in a dataframe
        • Asterisk Refer to new IP address
        • Refer a friend through sms getting White screen

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)
        • Privacy
        • Terms
        • Cookies
        • Homegardensmart
        • Aftereffectstemplates
        • Jogjafile