Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

Please write me some JS function that returns a string with comma seperated attribute…

  1. User

    Please write me some JS function that returns a string with comma seperated attribute values from each element on the page that has the attribute "data-adid".

  2. Assistant

    Here is a possible solution:

    function getDataAdIds() {
      var elements = document.querySelectorAll('[data-adid]');
      var ids = Array.from(elements).map(function(element) {
        return element.getAttribute('data-adid');
      });
      return ids.join(',');
    }
    

    This function first uses the querySelectorAll method to select all elements on the page that have the "data-adid" attribute. It then converts the resulting NodeList object into an array using the Array.from method.

    Next, it uses the map method to create a new array containing the values of the "data-adid" attribute for each element. Finally, it uses the join method to join the array elements into a comma-separated string, which is returned by the function.