Adding content between previous and next buttons with the use of JavaScript

After successfully implementing the functionality for the previous and next buttons in my script, I was requested to include the date between the buttons like this:

btnPrev issueDate btnNext
< Date >

Although I tried adding the date between the PREV and NEXT buttons in the code, it doesn't show up on the page. How can I properly insert text between those two buttons?

type: "span",
textContent: "November 1, 2022",
...this.btnIssue,
});

This is the complete script:

constructor(selector, options = {}) {
Object.assign(
this, {
EL: document.querySelector(selector),
page: 0,
selector,
// btnTabs: {}, // Custom attributes for tabs (navigation) buttons
btnPrev: {}, // Custom attributes for PREV button
btnNext: {}, // Custom attributes for NEXT button
classActive: "is-active",
onChange: () => {},
},
options
);
this.EL_pages = this.EL.children;
this.EL_pagination = document.querySelectorAll(`[data-tabs-pagination="${this.selector}"]`);
this.EL_navigation = document.querySelectorAll(`[data-tabs-navigation="${this.selector}"]`);
this.total = this.EL_pages.length;
this.EL_prev = this._ELNew("button", {
type: "button",
textContent: "<",
onclick: () => this.prev(),
...this.btnPrev,
});
this.EL_issue = this._ELNew("span", {
type: "span",
textContent: "November 1, 2022",
});
this.EL_next = this._ELNew("button", {
type: "button",
textContent: ">",
onclick: () => this.next(),
...this.btnNext,
});
this.EL_buttons = Array.from(Array(this.total)).reduce((arr, _, i) => {
const EL_btn = this._ELNew("button", {
type: "button",
textContent: i + 1,
onclick: () => (this._page = i),
...this.btnPagination,
});
arr.push(EL_btn);
return arr;
}, []);
this._init();
}

// Utility function - New element
_ELNew = (sel, attr) => Object.assign(document.createElement(sel), attr || {});

// Fix negative modulo index 
_mod = (n) => ((n % this.total) + this.total) % this.total;

// Initialize
_init() {
// Append nav buttons to DOM
this.EL_pagination.forEach((EL) => EL.append(...this.EL_buttons));
this.EL_navigation.forEach((EL) => EL.append(this.EL_prev, this.EL_next));

// Set current page
this._page = this.page;
}

prev(n = 1) {
this._page -= n;
return this;
}

next(n = 1) {
this._page += n;
return this;
}

show(idx) {
this._page = idx;
return this;
}

set _page(n) {
this.page = this._mod(n);
[...this.EL_pages, ...this.EL_buttons].forEach((EL) => EL.classList.remove(this.classActive));
[this.EL_pages[this.page], this.EL_buttons[this.page]].forEach((EL) => EL.classList.add(this.classActive));
// Provide a callback
this.onChange.call(this);
}

get _page() {
return this.page;
}
}


// Use like:
const mySectionTabs = new Tabs("#issue-nav", {
onChange() {
console.clear();
console.log(`Current page index: ${this.page}`);
}
});

Answer №1

I accidentally omitted the reference to "this.EL_issue" and have rectified that mistake now.

this.EL_navigation.forEach((EL) => EL.append(this.EL_prev, this.EL_issue, this.EL_next));

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

most effective method for establishing a relationship between a variable and its corresponding identification number

When retrieving results from a database with a simple PHP while loop, one of the data pieces includes a number that links to another table where the corresponding value is stored. While there are various ways to link this information and display the relate ...

A guide on how to assign a placeholder as the default value for a date picker

Currently using Vue3 with options API, and this question does not pertain to date formatting. Looking at the code provided on StackBlitz here, the default format for the date being initially set is dd.mm.yyyy. I am interested in knowing how to set the date ...

Alter the Default Visibility on an HTML Page from Visible to Hidden with JavaScript

I found this code snippet on a website and made some modifications. On my webpage, I have implemented links that toggle the visibility of hidden text. The functionality is working fine, but the issue is that the hidden text is initially visible when the p ...

What could be causing the DIV to be out of place when the window is resized?

When the window is minimized, all elements remain intact except for the navigation. The image below shows the layout when the window is maximized, while the image above depicts the layout when the window is minimized. HTML: <header> <div c ...

Resolve issues with vue js checkbox filtering

As I embark on my Vue journey, I came across some insightful queries like this one regarding filtering with the help of computed(). While I believe I'm moving in the right direction to tackle my issue, the solution has been elusive so far. On my web ...

Creating a PDF document with multiple pages using a PHP loop, integrating with AJAX, and utilizing the m

Looking for assistance with a plugin development project involving PDF generation using AJAX. The challenge lies in generating multiple PDFs for each user within a loop. The goal is to create PDFs with multiple pages for individual users. If that's n ...

Troubleshooting: The Jquery each loop is malfunctioning

I am new to using jquery and I have encountered a problem in my project. I am attempting to iterate through all the links within the #rate_box and attach a click event to them. This click event is supposed to send data to an external php script, then remov ...

Secure your data with public key encryption and decryption using the Crypto Module in NodeJS

I have a challenge with encrypting/decrypting data using a public key that is stored in a file. The code snippet below illustrates my approach: encryptWithKey (toEncrypt, publicKeyPath) { var publicKey = fs.readFileSync(publicKeyPath, "utf8"); ...

What causes inline-blocks to malfunction after a non-breaking space is inserted?

Here is some HTML code: <p id='one'>Hello World how are you.&nbsp;<span>Entrepreneur</span></p> <p>Hello World how are you.&nbsp;<span>Entrepreneur</span></p> Below is some CSS styling: ...

Having trouble with adding an event listener on scroll in React JS. Need assistance in resolving this issue

I'm having trouble adding an event listener for when a user scrolls in my web app. componentDidMount = () => { let scrollPosition = window.scrollY; let header = document.getElementById("topBar"); window.addEventListener(&ap ...

personalized XMLHttpRequest method open

var open = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function(method, uri, async, user, pass) { this.addEventListener("readystatechange", function(event) { if(this.readyState == 4){ var self = this; var respons ...

What seems to be the issue with this jQuery form submission?

I am currently working on a form that needs to be submitted using Ajax in order to avoid reloading the page. Although the Ajax call is reaching the server, it doesn't seem to trigger the necessary function. I have attempted both $.post and $.ajax, bu ...

Tell me the permissions that a user possesses in discord.js

I need help creating a command using Discord.js that can display a user's permissions. For instance, the command could be $permissions @user and it should output something like: "User permissions within this guild: " I'm unsure if ...

The asynchronous callbacks or promises executing independently of protractor/webdriver's knowledge

Could a log like this actually exist? 07-<...>.js ... Stacktrace: [31m[31mError: Failed expectation[31m [31m at [object Object].<anonymous> (...06-....js)[31m[31m[22m[39m It seems that something is failing in file -06- while I am processin ...

Library for adjusting the x axis scaling accurately

I need to find a JavaScript chart library that can support lines and zooming for handling a large amount of data. One issue I have encountered with other libraries is scaling the x-axis based on date and time data provided in strings: y=[43,56,34,63....] ...

CSS: Position an element based on the size of the screen

I'm currently developing a program that will dynamically resize elements based on the viewer's screen size. The program is being built using JSP/SQL/XHTML/CSS, and I have a couple of queries. Is there a way to select a CSS file by storing the sc ...

Retrieving information from various datasets through inquiry

First Model const mongoose = require("mongoose"); const finalApprovalSchema = mongoose.Schema({ formId: String, designApproval: String, rejectionReason: String, date: { type: Date, default: Date.now, }, }); const FinalApproval ...

Accessing Data from the Wikipedia API

After receiving a JSON response with the following structure: { "batchcomplete": "", "query": { "pages": { "97646": { "pageid": 97646, "ns": 0, "title": "Die Hard", "extract": "Die Hard is a 1988 ...

Utilizing Wordpress post images to dynamically change background URLs in CSS

Is there a way to dynamically set a background image in CSS using PHP, specifically the Wordpress post image? background: <?php if(has_post_thumbnail()){ $img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'dest ...

Store <td> in a variable

At the moment, I have a script that allows users to input an item name, along with its size, color, and other options. Each of these entries is saved as individual items with their custom specifications, such as a black t-shirt in large size. The script c ...