0

I have below code snippet.

    <caption>TEST Value</caption>
    <table *ngIf="settings" class="table" id="datatable">

    <caption>TEST Value1</caption>
    <table *ngIf="settings" class="table" id="datatable">
    <caption>TEST Value</caption>
    <table *ngIf="settings" class="table" id="datatable">
    <caption>TEST Value2</caption>
    <table *ngIf="settings" class="table" id="datatable">
    <caption>TEST Value3</caption>
    <table *ngIf="settings" class="table" id="datatable">

Now I want to locate HTML element (which is table) whose just previous sibling value is as given. For example: I want to locate HTML element whose caption is TEST Value3, in that case it should return reference of that particular table. Notice

document.getElementById('datatable').previousElementSibling.innerHTML

gives me very first table by id and its sibling TEST Value. All tables have same ID, so document.getElementByID() will return very first table also and that's why I want to take advantage of caption to identify individual table. You can always ignore duplicate IDs and can use any selector like by tagname, etc.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Rahul Kumar
  • 353
  • 1
  • 3
  • 9
  • 2
    Duplicate IDs are invalid HTML, fix that first – CertainPerformance Apr 16 '20 at 07:12
  • Does this answer your question? [JavaScript and getElementById for multiple elements with the same ID](https://stackoverflow.com/questions/3607291/javascript-and-getelementbyid-for-multiple-elements-with-the-same-id) – Heretic Monkey Dec 27 '20 at 02:21
  • If the Html keeps the pattern of caption followed by table then just query both and use indexing to find table under the index for the caption you want. – JWP Dec 27 '20 at 03:10

0 Answers0