Search code examples
htmlhyperlinkanchor

Link to one of many divs with the same class?


Sometimes I want to send a link to e.g. an article in a newspaper and want the receiver to read a specific paragraph, chapter or similar. If the structure of the page is something like

<p class="h2">Some header
    <p class="body">Body text

etc. Is it possible to link to any of these element specifically? For example, I want someone to read the fourth paragraph, that is, the fourth time the class="h2" is used, something like . I know, but I don't recall the syntax ATM, that XPaths offers this functionality, but what about html?


Solution

  • Google Chrome offers this functionality since a couple of months ago. Right-click on whatever you want to direct link to and select Copy link to. The result is something like this:

    https://stackoverflow.com/questions/21036942/link-to-one-of-many-divs-with-the-same-class#:~:text=If%20you%20visit-,following,-link%2C%20this%20will

    This is not supported on all browsers so far but I guess it, with time, will spread.