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.