Using id attribute from parent element
See original GitHub issueI have a page structure like this:
<section id="section1">
<h3>Section 1</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</section>
Is it possible to apply AnchorJS to the h3
elements, while using the existing id
from its parent section
?
The obvious solution anchors.add('section h3');
would generate new id
attributes for the h3
tags which I would like to avoid. And using anchors.add('section');
will add the anchors after the section and no the the h3
.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
arrays - How can I get an element's attribute from the parent's ...
This work fine pure javascript: document.getElementById("col1").getElementsByTagName("img")[0].getAttribute("src");.
Read more >jQuery: How can I find the ID attribute of a parent element with ...
From your current example, I believe you want to get id “apple” from “#test” element. You can select its parent with 3 main...
Read more >Get Attribute of a Parent Node Using JavaScript - Linux Hint
The “parentElement” property gives the parent element of the associated element. Whereas the “getAttribute()” method returns the value of an element's attribute ......
Read more >Selectors - W3C
The ID attribute of a document language allows authors to assign an identifier to one element instance in the document tree. CSS ID...
Read more >jQuery parent() Method - W3Schools
The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
WAIT! I just remembered! We recently added a feature for these kinds of situations that hadn’t been documented (or released) yet!
In short, you can use a
data-anchor-id
attribute pointing to theid
you want it link to:There’s more details in that PR. You can find the code in the master branch, but it isn’t in a release yet. Let me see if I can get a new release (and updated docs) out this weekend.
Ok, I just released 4.0.0, and the docs for this new feature can be found here: https://www.bryanbraun.com/anchorjs/#section-ids. Let me know if there are any other questions.