14
Views
8
Comments
Couldnt read the element details using both id/class inside JavaScript widget
Discussion


have a onclick action and inside that calling the JavaScript widget. Can you guys please help me to fix this issue.

2024-07-12 05-57-50
Gourav Shrivastava
Champion

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

UserImage.jpg
Manimaran Keppan

its fixed thanks


2020-11-25 10-45-32
Mostafa Othman
Champion

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.

2024-10-03 06-30-47
Bharathiraja Loganathan

 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]);

    } else {

        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

UserImage.jpg
Manimaran Keppan

thanks @Bharathiraja Loganathan  its working


2024-10-03 06-30-47
Bharathiraja Loganathan

please mark as solution @Manimaran Keppan 

2023-02-19 05-11-08
Faais

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

UserImage.jpg
Manimaran Keppan

@Faais  now the issue solved. used @Bharathiraja Loganathan 's response and tried its working now


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.