0

I'm using scrollIntoView function to click on a link and have it jump to a different part of my web app. The issue i am having is that i dont know how to target an HTML element called 'identifier'

so my html looks like...

<div class="subpara" identifier="2b">
            <num value="b">(B)</num>
            <content>some conent</content>
</div>

I want to be able to target the 'identifier' 2b in this case

i tried using:

  onClickOutlineNav(id) {
    let element = document.getElementById(id);
    //scroll to identifier
    element.scrollIntoView();
  }

and it doesnt seem to be working..any ideas?

jeremy
  • 433
  • 1
  • 8
  • 30

1 Answers1

3

You're using incorrect html tag syntax, which would be your first problem.

<div class="subpara" id="2b">

The getElementById function looks for the "id" property on html tags, not the "identifier" property.

If you insist on using the "identifier" property, you can query for it like so:

let element = document.querySelector('[identifier="2b"]');

or more generically:

let element = document.querySelector(`[identifier="${id}"]`);
Michael
  • 581
  • 2
  • 8