have a onclick action and inside that calling the JavaScript widget. Can you guys please help me to fix this issue.
Hello @Manimaran Keppan ,
Most probably it happens when the class and ID were not correct or the DOM is not loaded before the JS call. Can you please share the OML so I can help with the implementation
Thanks
Gourav Shrivastava
its fixed thanks
Hello,
It seems that line which return elements by class name doesn't return any element so var el1 is null and you can't use classList with undefined object.
Try to make sure you are using correct class name and also you may use if condition to check if el1 is null or no before call its items properties.
follow this step you will get a Solution: Use setTimeout() to delay execution after DOM is ready
1. Update Your JavaScript Code
Use this inside your JavaScript widget:
setTimeout(function () {
var el = document.getElementById($parameters.FAQElementId);
console.log("Element by ID:", el);
if (el) {
console.log("el.className:", el.className);
} else {
console.warn("Element with given ID not found.");
}
var el1 = document.getElementsByClassName("faq-dropdown-content");
if (el1.length > 0 && el1[0]) {
el1[0].classList.add("mystyle");
console.log("Added 'mystyle' to:", el1[0]);
console.warn("No element found with class 'faq-dropdown-content'");
}, 150); // 150ms delay ensures DOM is ready
2. Where to Place the JavaScript Widget
Place the JavaScript widget at the bottom of the screen or inside the same container as the target element.
Ensure the element with the class faq-dropdown-content is already rendered when JS runs.
3. Set Input Parameter Correctly
If you’re using el = document.getElementById($parameters.FAQElementId); Make sure you pass the actual Element ID to the JavaScript widget as an input parameter.
Example:
Add a local variable or widget ID (e.g., Container_FAQ.Id)
Pass it to the JavaScript widget's FAQElementId input
thanks @Bharathiraja Loganathan its working
please mark as solution @Manimaran Keppan
Hi Manimaran, You might have called the wrong class name. Could you please tell me where you are calling this JavaScript node? Is it in OnReady? Also, are these class names inside a list?
Just check with the steps below:
* Check if the class names are correct.
* If the class names are inside a list, use setTimeout with a delay of 0.
* If your blocks have many instances, consider using a MutationObserver inside that javascript code
@Faais now the issue solved. used @Bharathiraja Loganathan 's response and tried its working now