Looking to dynamically increase the ID of a specific DIV element using Jquery at regular time intervals?

<div id='my_id1'></div>

Is there a way to increment by 1+ in a loop at specific time intervals, such as on the same div after 3 seconds using jQuery? I am new to JavaScript and would appreciate any help. Thank you!

Answer №1

let count = 0;
setInterval(() => { 
    count = $('div').attr('id');
    count = count.substring(count.indexOf('d')+1);
    count = parseInt(count,10);
    count += 1;
    $('div').attr('id' , 'my_id_' + count);
    console.log(count);
}, 3000);

Does this meet your requirements?

https://jsfiddle.net/9bcjn3sm/4/

Answer №2

let count=0;
let timer=setInterval(function() { 
  count++;
  console.log("<pre><div id='unique_id"+count+"'></div></pre>");
  if(count===5){
    stopTimer();
  }
}, 1000);

function stopTimer(){
  clearInterval(timer);
}

The code above is functioning properly, you can see a Demo of it in action.

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

What is the best way to adjust the z-index of a dropdown auto complete to appear above a background image?

Is there a way to adjust the z-index of the drop-down autocomplete box so that it appears above a background image? Take a look at this screenshot: https://i.sstatic.net/H90jV.png The four images below are background images styled with sprite CSS. The a ...

Menu Design Inspired by Amazon Using jQuery

Can someone please assist me in locating a jQuery Amazon-style menu example? I would like to implement something similar and am hoping to avoid having to write it from scratch. Thank you! ...

Creation of a Joomla module

I'm currently working on a simple module using jQuery, however I am facing issues with disabling MooTools. In my attempt to resolve this, I utilized the following code in the default.php file: $user =& JFactory::getUser(); if ($user->get( ...

Convert all relative path URLs in images to absolute paths using either PHP, javascript, or jQuery

I am looking to transform any relative URL found on a webpage, with the main intention of resolving issues related to images not being displayed. The goal is to convert these relative URLs into absolute URLs. For example: From <img src="/folder/folder ...

Issue: AngularJS modal not appearing when utilizing partial for template URLExplanation: The Angular

I am having trouble with a modal in a partial file that is supposed to be loaded into my main view using an ng-include tag. However, the template cannot be found and I do not see it being loaded in the console or network tab. The error message I receive is ...

Invoke a function from a neighboring component using React hooks

Is there a way to call a function from another component using props or context? I've attempted to do this using props and context, but I'm getting confused as I need to pass an actual function with parameters. The goal is to invoke the handleS ...

Is client-side rendering the new trend, bypassing traditional server-side rendering?

I'm exploring the idea of rendering my pages on the client side to reduce server load and minimize traffic. Typically, the first request to the server requires it to render the requested page and then send it to the user. Once the user interacts with ...

Discover the potential of JavaScript's match object and unleash its power through

In the given data source, there is a key called 'isEdit' which has a boolean value. The column value in the data source matches the keys in the tempValues. After comparison, we check if the value of 'isEdit' from the data source is true ...

Conceal a row using jQuery

On my current webpage, I have several fields displayed: Delivery Options: Standard Shipping Gift Message: vbxcxb Delivery Date: 12/06/2013 Age Verification: No These fields are generated server-side after a form is completed and the HTML code for them ...

Off-center rotation of an element - seeking solution [closed

This question has been closed. It requires additional details for debugging. Answer submissions are currently not accepted. ...

You are unable to assign mutations in Vuex

Dealing with a peculiar problem where "val" and "ok" can be used within "console.log()", but for some reason, state.user cannot be assigned any value. However, state.user does display 'ok' on the website. export const state = () => ({ user: ...

Steps to add the following level of JSON data to a tree-view format

I am currently working on appending JSON data to create a tree view structure. Initially, I created a static tree view, and you can find the code for it in this fiddle: https://jsfiddle.net/ak3zLzgd/6/ I am facing challenges while trying to append thre ...

How can I use the store command to retrieve the href value of a link using a css selector in Selenium?

Is there a way to store a URL from a link using CSS instead of xpath? store //tr[td[contains(.,'6 Day')]][1]/td[8]/a@href my_var open $my_var If so, how can I achieve this goal with css? I managed to use the following locator: store css= ...

Dynamically hiding elements within tabs using jQuery

Previously, I created a functionality for handling a single set of tabs. However, as the application has expanded, this functionality is no longer sufficient to accommodate multiple sets of tabs. The initial function looked like this: var iconTabs = func ...

Enabling and Disabling Input Fields Based on Selections in VueJS

Originally submitted on es.stackoverflow.com by José: I came across an example in JavaScript that works, but I'm struggling to implement it in vue.js. I've been attempting it for some time without success. Apologies for any inconvenience. < ...

What is the best method for sorting comments by ID using jQuery?

I am embarking on a new project to create a filter for comments so that they are displayed with the corresponding post ID. Your help is greatly appreciated! $(function () { $.ajax({ type: "GET", url: "https://jsonpla ...

Error Occurred while Uploading Images using Ajax HTML Editor with JSON Data

I am facing an issue with my AJAX HtmlEditorExtender, specifically when trying to upload an image. The error message I receive is as follows: JavaScript runtime error: Sys.ArgumentException: Cannot de-serialize. The data does not correspond to valid JSON. ...

Creating a popup window using HTML

I am attempting to create a pop-up window using HTML, but I'm struggling to get it to appear in the desired way. My goal is this: to have a pop-up show up when the profile image is clicked on. Here's the HTML <span class="profile"><im ...

Is this code correct for passing a variable to another form?

$("#delete").click(function() { deleterecord(); }); function deleterecord(){ var id = $("#iduser").val(); alert("aw"+id); var id = $('#iduser').attr(); e.preventDefault(); pressed = "delete" $.ajax({ ...

The socket.io client in my JavaScript code is failing to receive the necessary event

Currently, I am in the process of configuring a socket.io chat feature with an expressjs backend and sveltejs frontend. I have established a custom namespace named 'chat' where a new room is generated upon a 'join' request. My appro ...