Hi,
we are using tool tip for the smaller fields and we are adjusting this tool tip position manually, we want to adjust the tool tip position dynamically, tool tip position is not proper when there is long text
To adjust the position of the tool tip, we are using this below approach
Codeconst hoverElement = options.htmlElement;
hoverElement.addEventListener("mouseover", (event: MouseEvent) => {
const target = event.target as HTMLElement;
console.log('target:',target);
if(target.classList.contains('sd-input') || target.classList.contains('sd-dropdown')){
const currentText = target.innerText || target.getAttribute('value');
if (!currentText) return
this.popupText = currentText && currentText.trim();
const { left , top , width } = event.target['getBoundingClientRect']();
const tooltipWidth = 1036;
const tooltipLeft = left + width / 2 - tooltipWidth / 2;
const tooltipTop = top - 42.00;
this.popupPosition = {
top: `${tooltipTop}px`,
left: `${tooltipLeft}px`
}
this.toolShowPopup = true;
this.cdRef.detectChanges();
}
});
Code<ng-container *ngIf="toolShowPopup">
<div class="onHoverTooltip" [ngStyle]="{ 'left': popupPosition.left, 'top': popupPosition.top }">
<span class="tooltip-text">{{ popupText }}</span>
<span class="beak beak_top"></span>
</div>
</ng-container>
Please tell me the best approach to achieve this
Thanks,
Surya
Hello Surya,
SurveyJS forms do not have a dedicated API for applying custom components as form element tooltips. However, you can use the
survey.onAfterRenderQuestionInput
event to attach custom tooltips to SurveyJS elements. This function allows you to access the corresponding HTML form element (e.g., a question input or a cell) and modify it as needed.Regarding your specific question: please note that tooltip functionality is not directly tied to SurveyJS components. Once you obtain the underlying HTML element, you can retrieve its properties and calculate the tooltip's position and dimensions as required. However, by looking at your code, I noticed that the
tooltipWidth
is set to a fixed value of1036
. This may lead to incorrect positioning if the tooltip's actual width changes, such as when displaying long text. To ensure proper alignment, consider dynamically calculating the tooltip's width usingtooltipElement.offsetWidth
or a similar method after rendering.Thank you