Trouble Getting Results with getElementByClassName

I am facing an issue with my code. Originally, I was using getElementById(), but since the function needs to apply to multiple links, I switched to getElementByClassName(). However, it isn't returning any results. I have provided all the details on jsFiddle

Here is a snippet of the javascript:

var myBoxWidth = 0;
var myBoxWidth2 = 0;

// show
function show() {
    var myBox = document.getElementByClassName('box');  
    var myContent = document.getElementByClassName('content');
    myContent.style.display = 'inline';
    myBox.style.width = myBoxWidth + '%';  
    if(myBoxWidth < 80) {  
        myBoxWidth += 20;
        setTimeout(show,55);
    }
}

// hide
function hide() {
    var myBox = document.getElementByClassName('box');
    var myContent = document.getElementByClassName('content');
    myContent.style.display = 'none';
    var currentWidthVal = parseInt(myBox.style.width,10);
    if(myBoxWidth2 < currentWidthVal) {  
        setTimeout(hide,55);
        myBox.style.width = currentWidthVal =  currentWidthVal - 20 + '%';
        myBoxWidth = 0;
    }
}

Answer №1

It appears that there is no function called getElementByClassName(). You may want to consider using getElementsByClassName() instead.

Update

document.getElementsByClassName('..')
actually returns a collection of elements, not a single element as your code seems to expect. To correct this, you can modify your code like so:

var myContent = document.getElementsByClassName('content');

var num = myContent.length;

for(var x=0; x < num; x++){
    myContent[x].style.display = 'block'; //or apply whatever styles you had in your original code 
}

Answer №2

Instead of using getElementByClassName, it is better to use getElementsByClassName. I would also suggest utilizing jQuery or another JavaScript framework for enhanced functionality.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customize the color of the label and underline in a Reactjs material-ui TextField when the field input

https://i.stack.imgur.com/oJ2ah.png <TextField id="standard-full-width" label="Password" style={{ margin: 8 }} fullWidth margin="normal" placeholder="*******" /> Struggling to understand how to modify the color of the label ...

Clicking to remove added child element

I have written a script that creates an image popup when the site loads. Here is the code: <script type="text/javascript"> function showPopup() { var div = document.createElement('div'); div.className += 'popup'; div.inne ...

Tips on implementing smooth-scroll scrollLeft in Vue3 with JavaScript

Currently, I am in the process of creating a horizontal scroll menu for displaying images. The navigation functionality includes arrow buttons for scrolling: <template> <section id="artwork" class="artwork"> <div cl ...

Obtaining a Variable Element through Selector

When working with Puppeteer, I am faced with the challenge of clicking on a web button that has a dynamic id like: #product-6852370-Size. Typically, I would use the following code: page.click('#product-6852370-Size'); However, the number withi ...

AngularJS: Initiate a Bootstrap modal upon webpage loading

As I work on designing a web application using AngularJS, I aim to implement the launching of a bootstrap modal upon page load through AngularJS. Here is the structure of my modal: <div class="modal hide fade" id="myModal"> <div class="moda ...

Ways to assign scores to every response button

Below is an excerpt of code that showcases a list of potential answers for each question in the form of checkbox buttons. The task at hand is to assign marks to each answer button, which can be retrieved from the database. Marks for correct answers are obt ...

Unable to iterate through the Array-like Object

I have an array of product objects that was retrieved from the server. return response()->json(['products' => $products->toArray()]); Here is a snapshot of the log: https://i.sstatic.net/hAzpr.png To extract the attributes from each ...

Encountering the following issue: Unable to load XMLHttpRequest for the function javascript:newDoc()

I've been experiencing an issue with my script that opens custom subdomains which were previously working fine. Since I integrated ajax for the contact form, it's not functioning properly. ERROR:(jquery.js:4 XMLHttpRequest cannot load javascri ...

Discovering Consecutive Matching Positions in an Array

I am trying to find the first location (or set of locations) in an array that is distinctly different from the initial location. The difference is determined based on a function calculating their distance. Here's an example array: [ {lat: 45, lng: ...

Ways to update the hidden field's value within a foreach loop when the change event occurs?

On my page, I have input fields and foreach conditions set up. <form action="process.php" name="employee" method="post"> <input type="text" name="name" class="onchangefield"> < ...

Please provide me with the steps to relocate the src/pages/api folder to src/ in a Next.js project

When working with Next.js, I am interested in relocating the src/pages/api directory to be located under src/, much like how it is done in blitz.js. In this case, the directory structure would look something like this: src |- pages |- api Unfortunately, I ...

The hidden attribute of UIWebView and its interplay with JavaScript

In the webViewDidStartLoad method, I hide the webview. Then a request is made. In the webViewDidFinishLoad method, I use stringByEvaluatingJavaScriptFromString. Finally, the webview is shown again. However, when I run the app, I can still see how the Java ...

Having trouble getting DataTables to function when using jQuery's .post() method

Currently, I am in the process of developing a form that allows users to select a date range for displaying information using DataTables. Upon clicking the button, the selected dates are sent via jQuery's .post() function to retrieve the desired data ...

Exploring the concept of $http/$q/promise in Angular

I am struggling to understand when $q/$http should trigger the onReject block. For example, if I have a simple call like this: $http.get('/users') .then(function(res) { return res.data; }, function(err) { console.log(err); }); If ...

What is the best way to convert a promise using an async await function into an observable?

Currently, I have set up a Nestjs rest server that includes a controller and a service. Within the controller, there is a get function that is triggered when a get request is made: @Get() getAllFoos() { return this.fooService.getAllFoos(); } Inside ...

Error 403 Forbidden on Selenium grid due to proxy issue

While I have been conducting website tests using Selenium webdriver on my local machine, I now aim to perform these tasks on a Windows EC2 instance. After researching, I discovered that Selenium Grid2 can be utilized on EC2 servers. However, upon setting u ...

I am configuring Jest in my Vite and TypeScript-powered React project

I am having trouble with the relative path of the file I imported in App.test.tsx. It keeps showing me this error message: Cannot find module '@/components/items/card.tsx' from 'src/__tests__/App.test.tsx' Below is the code snippet: // ...

Error Alert: jQuery AJAX Event is not defined in Firefox Browser

This snippet works perfectly on Webkit browsers, but for some reason, it's not functioning in Firefox. I have a feeling that I need to include "event" within parentheses somewhere, but I'm unsure about the correct placement. Any ideas? html < ...

"Trouble arises when attempting to duplicate an array with string indexes using jQuery

I am attempting to duplicate an array named ar, which contains string indexes, into another array called arCopy using jquery. You can view the structure of the array here. Initially, I tried copying arrays like this: var arCopy = ar; However, when I atte ...

Incorporate a Three.js viewer within a WPF application

I am currently exploring the use of Three.js to develop a versatile 3D renderer that can run seamlessly on various platforms through integration with a "WebView" or "WebBrowser" component within native applications. I have successfully implemented this sol ...