JavaScript and jQuery Conceal and Reveal

I'm having trouble with my jQuery hide and show functions. They seem to be working, but the items are not appearing on the page. I suspect it could have something to do with a parent element being positioned relative or having a display of none, but I haven't been able to identify the issue. Below is the code snippet:

HTML:

<div id="newsItem1">
    <h4><a href="/news">News Title 1</a></h4><h4>April 6, 2015</h4>
    <p>Text here</p>
</div>

<div id="newsItem2">
    <h4><a href="/news">News Title 1</a></h4><h4>April 6, 2015</h4>
    <p>Text here</p>
</div>

CSS:

#newsItem1, #newsItem2 {
    display:none;
}

JS:

 setInterval(NextNewsItem, 8000);
 var newsItem = 0;
 var numNewsItems = 2;

 function NextNewsItem() {
     newsItem++;

     if (newsItem > numNewsItems) {
         newsItem = 1;
     }

     console.log(newsItem);

     for (i = 1; i <= numNewsItems; i++) {
         console.log($("#newsItem" + i).css('display'));
         $("#newsItem" + i).hide();
         console.log($("#newsItem" + i).css('display'));
     }

     console.log($("#newsItem" + newsItem).css('display'));
     $("#newsItem" + newsItem).show();
     console.log($("#newsItem" + newsItem).css('display'));
 }

The first iteration shows:

1

none

none

none

none

none

block

Answer №1

CodePen

Everything seems to be running smoothly on my end.

HTML

<div id="article1">
    <h4><a href="/articles">Article Title 1</a></h4><h4>October 10, 2021</h4>
    <p>Content goes here</p>
</div>

<div id="article2">
    <h4><a href="/articles">Article Title 2</a></h4><h4>October 11, 2021</h4>
    <p>More content goes here</p>
</div>

JS

 setInterval(NextArticleItem, 1500);

 var articleItem = 0;
 var numArticleItems = 2;

 function NextArticleItem() {
     articleItem++;

     if (articleItem > numArticleItems) {
         articleItem = 1;
     }

     for (i = 1; i <= numArticleItems; i++) {
         $("#article" + i).hide();
     }

     $("#article" + articleItem).show();
 }

Please keep in mind that the styling is applied to all elements except the first one for toggle 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

Ways to display and conceal menu depending on initial view and scrolling

On my website, I have implemented two menus - one to be displayed when the page loads and another to show up when a scroll occurs. You can view my page here. The goal is to have the white part visible when the position is at the top, and switch to the blu ...

Unable to retrieve parameter while making a POST request

Need some help with attribute routing. I'm having trouble getting parameters from the HTTP body. The ConnectionID Class includes a property named CValue. $('#btn').click(function () { $.ajax({ type: "POST", url: "http:// ...

Establishing a connection to a remote Mongo database using Node.js

I have a remote MongoDB server and I am looking to establish a connection with it using Node.js. I was able to successfully connect directly with the Mongo shell, but for some reason, I cannot establish a connection to the MongoDB server in Node.js. The co ...

Tips for eliminating the undefined/null values from an array nested within another array in Angular

DATA = [{ application: [{ name: 'Room1' },{ name: 'Room2' },{ name: 'Room3' },{ name: 'Room4' },{ name: 'Room5' }], name: 'Batch 1&ap ...

Scraping Morningstar.com using Selenium and XPath in Python

I'm attempting to extract data related to mutual funds and ETFs from Morningstar.com using the Selenium library, but I'm having trouble with the following code: from selenium import webdriver driver = webdriver.Chrome() link = "https://www.morn ...

Is there a way to enable autofill functionality if an email already exists in the database or API within Angular 12?

In order to auto-fill all required input fields if the email already exists in the database, I am looking for a way to implement this feature using API in Angular. Any guidance or suggestions on how to achieve this would be greatly appreciated. ...

"CSS background not loading properly on jQuery infinite scroll feature, affecting the loading of

Currently, I am using the most recent version of infinite-scroll developed by paulirish. Everything seems to be working correctly, but there is a peculiar issue that I have encountered. Consider this scenario: You have a forum where infinite scroll is imp ...

Nested Bootstrap structure with a column containing two sub-columns

I am attempting to create a grid layout using bootstrap. The layout should consist of a full 12 column layout on desktop, with an 8 column grid and a 4 column grid within it. The 8 column grid will contain an image/text, while the 4 column grid will have t ...

Getting the Correct Information from Upload Files in jQuery and PHP

While experimenting with the jquery plugin called jquery.filer, I encountered an issue during file upload on their official website . The returned value I received looked like this: { "files": [ "..\/uploads\/dK079QrL2g.docx" ], "metas": [ ...

Default Filter for Lookup Dialog in Dynamics CRM 2011

I am attempting to customize a lookup dialog to default to a specific entity type. While working on the Connection form, I am checking the entity type of record1id and attempting to use that information to set the defaulttype attribute on record2id. My c ...

You cannot invoke this expression while destructuring an array of React hooks in TypeScript

Within my React TypeScript component, I have several fields that check a specific condition. If the condition is not met, the corresponding field error is set to true in order to be reflected in the component's DOM and prevent submission. However, whe ...

A guide on showcasing the compilation results of PHP code directly on the web browser

How can I compile PHP code on an HTML Button click and display the compilation output in a browser? The code snippet below works well in the terminal but doesn't show anything in the browser. I am currently using XAMPP server on a Windows machine. &l ...

Troubleshooting Problems with POST Requests in ExpressJS

Currently, I am working on developing a feature in NodeJS that allows users to upload files. However, I am encountering difficulties while attempting to make a simple POST request. In my index.ejs file, I have written code that generates a form and initia ...

What is the best way to enable padding outside of my iframe?

Currently, I am attempting to create padding around my iframe in order to position it correctly on a specific part of my webpage. However, I seem to be having trouble figuring out what mistake I am making and searching online is only serving to confuse me ...

Tips for sending a tab id to a URL using jQuery

Upon examining the code snippet below, it is apparent that I am attempting to pass the value of a tab's id to a URL. In this instance, I am displaying it in HTML just for illustrative purposes; however, the hashtag id fails to be transferred to the UR ...

Implementing a Radial Cursor with a Custom Background Image in JavaScript

I am attempting to implement a radial cursor on a website that features a background image. Currently, I am facing two main issues: The radial cursor works in Chrome but not in Firefox. When using Firefox, I encounter a parsing error related to the "bac ...

Testing a jQuery button click using C# Selenium RC

Is there a proper way to execute a jQuery .click() function in a Selenium test? The clickable element in question is a hyperlink. HTML: <div id="buttons"> <a class="button" id="btnRun" href="javascript:void(0)">Run</a> </div> ...

I can't seem to figure out which of these 4 statements is free of syntax errors

Can you identify which code block does not contain a syntax error? Out of the 4 options below, one of them is free from any syntax mistakes. alert("hello "+3+" times); alert("hello "+3 times); alert("hello +3+ times"); alert("hel ...

Contrasting actions when submission occurs by pressing ENTER versus clicking the submit button

Incorporating an HTML form input element that utilizes a SearchBox from the Google Maps Places API for auto-completion is proving to be quite challenging. Within my JavaScript code, I have instantiated a SearchBox as shown below: var input = $('#spot ...

The data from the Subscribe API call is gradually loading within the ngOnInit() function

When using Angular 8, I am experiencing slow data retrieval when making API calls in the ngOnInit() function. The issue arises when trying to pass this data as @Input from one component module to another - it initially comes through as undefined for a minu ...