Insert content into a div using JavaScript

Unable to display text inside a div using Javascript countdown code. The issue arises when attempting to insert additional text, such as " : ", within the time (hh:mm:ss) format. An example scenario is adding text before the time (car_1 12:52:13 remaining), which should automatically disappear once the countdown reaches zero. The following code showcases two countdowns, with the remaining time specified in hours (

var dolazak1 = new Date(sad2.getFullYear(),sad2.getMonth(),sad2.getDate(),24,00,00);
)

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>
<script>
var d = new Date();
var n = d.getDay();
if(n == 1 || 2 || 3 || 4 || 5){
var timer1;
function cdtd1() {
    var sad1 = new Date();
    var dolazak1 = new Date(sad1.getFullYear(),sad1.getMonth(),sad1.getDate(),23,30,00);
    var timeDiff1 = dolazak1.getTime() - sad1.getTime();
    if (timeDiff1 <= 0) {
        clearInterval(timer1);
                document.getElementById(id).innerHTML = '';
    }
    var sekunde1 = Math.floor(timeDiff1 / 1000);
    var minute1 = Math.floor(sekunde1 / 60);
    var sati1 = Math.floor(minute1 / 60);
    var dani1 = Math.floor(sati1 / 24);
    sati1 %= 24;
    minute1 %= 60;
    sekunde1 %= 60;

    $("#dani1Box").html(dani1);
    $("#sati1Box").html(sati1);
    $("#minute1Box").html(minute1);
    $("#sekunde1Box").html(sekunde1);

    timer1 = setTimeout(cdtd1, 1000);
}

$(document).ready(function() {
     cdtd1();
});

var timer2;
function cdtd2() {
    var sad2 = new Date();
    var dolazak2 = new Date(sad2.getFullYear(),sad2.getMonth(),sad2.getDate(),24,00,00);
    var timeDiff2 = dolazak2.getTime() - sad2.getTime();
    if (timeDiff2 <= 0) {
        clearInterval(timer2);
                document.getElementById(id).innerHTML = '';
    }
    var sekunde2 = Math.floor(timeDiff2 / 1000);
    var minute2 = Math.floor(sekunde2 / 60);
    var sati2 = Math.floor(minute2 / 60);
    var dani2 = Math.floor(sati2 / 24);
    sati2 %= 24;
    minute2 %= 60;
    sekunde2 %= 60;
    $("#dani2Box").html(dani2);
    $("#sati2Box").html(sati2);
    $("#minute2Box").html(minute2);
    $("#sekunde2Box").html(sekunde2);

    timer2 = setTimeout(cdtd2, 1000);
}

$(document).ready(function() {
     cdtd2();
});
}
</script>

<style type="text/css">
#dani1Box{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
         display: inline-block;
}

#sati1Box{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
         display: inline-block;
}


#minute1Box{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
         display: inline-block;
}



#sekunde1Box{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
         display: inline-block;
}

#dani2Box{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
         display: inline-block;
}

#sati2Box{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
         display: inline-block;
}


#minute2Box{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
         display: inline-block;
}



#sekunde2Box{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
         display: inline-block;
}

h1{font-size:70px;
    color:#1f62a7;
    font-family:Sans-serif;
    display: inline-block;
}

</style>
  <center>
    <div id="sati1Box">></div>
    <h1>:</h1>
    <div id="minute1Box"></div>
    <h1>:</h1>
    <div id="sekunde1Box"></div>
    </div>


    <h1> </h1>

    <div id="sati2Box"></div>
    <h1>:</h1>
    <div id="minute2Box"></div>
    <h1>:</h1>
    <div id="sekunde2Box"></div>
    </div>

   </center> 
</body>

Answer №1

One of the useful features in jQuery is the prepend() method. This function allows you to add HTML content to an element before its existing content.

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

Utilizing AngularJS to invoke a particular function within a PHP script: A comprehensive guide

Utilizing AngularJS to interact with a PHP script, I am aiming to specifically call out individual functions. In the provided app.js script, the updatePost and deletePost functions are triggered by button clicks. While I've successfully separated the ...

The issue of data loss in a PDF file while using the FileReader

My current challenge involves the necessity of sending data to a server in JSON format exclusively, and I also need to include a PDF file along with other form data within the JSON. Initially, I attempted to convert the PDF into a base64 string following a ...

Modify the hover color of heading 1 only for hyperlink texts

I am attempting to adjust the hover color specifically for text that contains a link. Below is the CSS code I have tried, but it changes the color regardless of whether there are links present or not. h1, h2, h3, h4 { color:#3F3F3F; } h1:hover, h2:hove ...

Utilizing JavaScript and jQuery to merge the highlighted text selection with a span HTML element

function combine_highlighted_text(sel) { span = htmlSpanHlt(); if (sel.getRangeAt) { range = sel.getRangeAt(0); } span.appendChild(range.extractContents()); range.insertNode(span); } function htmlSpanHlt() { element = document.createElemen ...

The CSS Grid is too small to accommodate the Grid Item, even though there is ample space available

I'm currently working on a grid system where the first part (one to five) should be the opposite of the second part (six to ten). I have successfully set up the first part, but I'm struggling with fitting the tenth piece into the grid. I attempte ...

The jQuery code runs successfully on jsFiddle, but fails to execute on my website

I've created a login script that generates a username by combining the first and last names (on blur), eliminating spaces, and removing special characters. While everything functions correctly in jsFiddle, I encounter an error on my website indicatin ...

Learn the process of creating a calculator using JavaScript in ASP.NET

Hey there! I'm looking to create a calculator in asp.net. I'd like to use Java Script alongside asp without the need for html and ajax. Any suggestions or guides on how to do this? ...

Learn the process of uploading a file using FormData alongside multer in node js. Experience the issue of receiving undefined in req.file and { } in req.body

Is there a way to successfully upload a file using FormData along with multer in Node.js? I seem to be encountering issues as req.file shows undefined and req.body displays {}. Check out the HTML code snippet below: <input type="file" name=&q ...

Update the JavaScript and CSS files following an AJAX request with $.get

I am encountering an issue where my global CSS and JS files contain all the necessary definitions, but when I load new HTML blocks via AJAX $.get, these new elements do not receive the correct CSS/JS definitions. Is there a convenient way to refresh the ...

The scrollTop function is exclusive to Firefox

My goal is to create a parallax effect by animating the background position of a div based on the distance scrolled. While this works flawlessly in Firefox, it seems like webkit-based browsers are not cooperating. Specifically, they are not returning a val ...

Before computing the width, Next.js Swiper slide occupies the entire width

Check out the code snippet below: 'use client'; import { Swiper, SwiperSlide } from 'swiper/react'; import 'swiper/css'; import 'swiper/css/pagination'; export default function Component() { const cards = [{ id: 0 ...

Creating arrow indicators in navigation menus using CSS and JavaScript can be seen on websites like playframework.org

While browsing the site, I noticed that the navigation menu at the top has a unique feature - a small arrow that points upward for the currently selected section (Home, Learn, Download,...). I tried to figure out how they implemented it, but I couldn&apos ...

Do you know the method to make a Youtube iframe go fullscreen?

I am encountering an issue with developing an iframe where I am unable to make it go fullscreen from within the iframe. Fullscreen API works when both the iframe and hosting website are on the same domain, as well as triggering fullscreen from outside the ...

Unused DIV elements in jQueryUI are identified using XSLT

Just a heads-up, I'm new to xslt so please bear with me if this seems like a silly question. In my xsl:template I have created a div element like this: <div id="objectLocked"> This object is locked by another user. Do you want to remove the lo ...

The V-model binding feature seems to be malfunctioning on mobile devices, unless I incorporate an alert into the

Encountering a strange issue with form input binding in my Vue application. The input field is an auto-suggest search field with the following set up: <input class="input" type="text" v-model="search" @input="onChange" @keyup.up="onArrowUp" @keyup.down ...

Vue.js - The error message "$slots have 'el' is null" indicates a problem with the element in

When trying to access the Vuejs $slots instance, I encounter el = null, but type = "div" Here is the template: <slot name="head"> <h1> {{ text }} </h1> </slot> And in the script section: ... ...

Unlocking the secrets of retrieving the URL query string in Angular2

I'm struggling to extract the URL query string returned by the security API, resulting in a URL format like this: www.mysite.com/profile#code=xxxx&id_token=xxx. My goal is to retrieve the values of code and id_token. In my ngOnInit() function, I ...

Guide on redirecting a web page using jQuery

My function is to display a specific section on the same page when a value is selected from a dropdown list. All content IDs will be on the same page, so I need to navigate to that section based on the selected value. Dropdown list options: <sel ...

Is there a more efficient method for incorporating artists' animations in ThreeJS?

As of now, I am dealing with a collada file that contains animations created by the artist. My goal is to control specific parts of the animation using buttons on my webpage. Do you think collada is the right format for this task, or should I consider us ...

ASP.NET WebMethod sends the entire webpage back to JQuery ajax requests

I've been working on a web application that involves calling an ASP.NET WebMethod from jQuery when a textbox is clicked. However, I'm encountering an issue where the WebMethod is returning the entire ASPX Page instead of just the values I need. H ...