Rich
Rich

Reputation: 15465

Find the nearest named anchor to an element on a web page in JavaScript

I'd like to be able to right-click on an element on a web page and get the URL of the nearest anchor.

For example, say I'm looking at online documentation, and I see a paragraph saying something surprising. Then I'd like to be able to right-click on that, choose "copy URL of nearest anchor", go to my IDE, and be able to paste a URL, like "http://docs.somelang.org/1.9/manual.htm#section9" into a code comment.

Currently, I have to do "view source" and hunt around for the nearest element.

Anything which does something reasonably close to this in any major browser would suffice, but my ideal answer would be something which can be added to the right-click menu in Chrome.

Upvotes: 4

Views: 1772

Answers (2)

Rich
Rich

Reputation: 15465

There's a bookmarklet here which does this: Named anchors

Upvotes: 3

sandstrom
sandstrom

Reputation: 15092

You could write a jQuery script that you run on each page, triggering on clicks.

  1. Use jQuerify or similar to activate jQuery on the page.
  2. Then use the console of Chrome, Safari or Firefox to enter your code.
  3. Code could begin along these lines: $('body').live('click', functionThatFindsClosestAnchorElement);

Hope this helps!

Upvotes: 0

Related Questions