Hosting & Domaining Forum

Hosting & Domaining development => Programming Discussion => Software & Scripts offers & requests => Topic started by: miaedwards on Sep 03, 2022, 01:43 AM

Title: Help me fix this script
Post by: miaedwards on Sep 03, 2022, 01:43 AM
A basic script that loads desired text (date) from a designated element can be rephrased as follows without losing meaning:

In the code snippet below, the desired date is loaded and displayed from a specific element with the following HTML code:

<span id="data">date loading...</span>
This is achieved using the following script:
<script>
elm = dоcument.getElementById('data');
elm.innerHTML = "May 19, 21-00"
</script>

However, if there are numerous elements of interest on the page (such as replacing all spans designated by class="data"), a different approach must be taken. Despite the availability of dоcument.getElementsByClassName, it may not work on all browsers, including mobile devices.
Title: Re: Help me fix the script
Post by: nancyfromafrica on Sep 03, 2022, 02:03 AM
Based on the code provided, it appears that there is an ID labeled as "data" rather than a class. Note that only one ID should be present.

To cycle through all data on the page, a script must be implemented. The following code shows a simple script to loop through desired elements with the "data" class:

<span class="data">date loading...</span>
<span class="data">date loading...</span>
<span class="data">date loading...</span>

The code below should be inserted after the desired elements in the page code, which will execute the script through all "data" elements in a loop:

<script type="text/javascript">
var elem = dоcument.querySelectorAll('.data');
for (i = 0; i < elem.length; i++) { elm[i].innerHTML='May 19, 21-00'; }
</script>

For beginners in scripting, note that using the querySelectorAll() method can simplify searching for specific elements with designated classes. Additionally, testing scripts on multiple browsers and devices is essential to ensure cross-compatibility.
Title: Re: Help me fix this script
Post by: ichnolite on Sep 28, 2024, 01:50 AM
Using dоcument.getElementsByClassName may not be the most reliable or efficient solution, especially when dealing with a plethora of elements on the page. This is due to the fact that older browsers, including some mobile devices, may not support this method, leading to a fragmented user experience.

A more robust and cross-browser compatible approach would be to utilize the power of querySelectorAll, a method that allows you to select multiple elements based on a CSS selector. This approach is more flexible and efficient, especially when dealing with complex HTML structures.

For instance, if you have multiple span elements with the class "data" that you want to update with the desired date, you can use the following script:

const dataElements = dоcument.querySelectorAll('.data');
dataElements.forEach((element) => {
  element.innerHTML = "May 19, 21-00";
});

This script uses the querySelectorAll method to select all elements with the class "data" and stores them in a NodeList. Then, it uses the forEach method to iterate over each element in the NodeList and updates its innerHTML with the desired date.

It's essential to consider the browser compatibility and performance implications of your code. By using querySelectorAll, you can ensure that your code is more robust, efficient, and compatible with a wider range of browsers and devices.

Moreover, if you're working with a large number of elements, using a more targeted approach like querySelectorAll can also improve the performance of your code. This is because it allows you to select only the elements that need to be updated, rather than iterating over the entire DOM.
Title: Re: Help me fix this script
Post by: Nabodubpad on Jun 03, 2025, 02:00 AM
Consider using a loop to iterate through the selected elements and update their inner HTML. Here's a revised version of your script:

const elements = dоcument.querySelectorAll('.data');
elements.forEach(elm => {
    elm.innerHTML = "May 19, 21-00";
});

This approach ensures that all elements are updated efficiently. If you're still relying on getElementsByClassName, it's time to modernize your code. Sticking with outdated methods can lead to maintenance headaches and hinder your project's scalability.