Tips for refreshing a specific <div> element upon clicking the shuffle button without having to reload the entire page

I need help with a simple dice game coded in HTML, CSS, and JS.

Currently, whenever I reload the page or click the shuffle button, the entire page refreshes causing the dice images to change. However, I want only one specific div ("context div") to refresh, as it contains an animated background that starts from the beginning each time. I don't want the background to reset every time the page reloads, so I'm looking for a solution that will only refresh the dice images inside the div. Can anyone assist me with this?

Here is the code snippet:

var randomNumber1 = Math.floor(Math.random() * 6) + 1;

var randomDiceImage = "dice" + randomNumber1 + ".png";

var randomImageSource = "https://raw.githubusercontent.com/JallaJaswanth/Dice-Game/main/images/" + randomDiceImage;

var image1 = document.querySelectorAll("img")[0];

image1.setAttribute("src", randomImageSource);


var randomNumber2 = Math.floor(Math.random() * 6) + 1;

var randomImageSource2 = "https://raw.githubusercontent.com/JallaJaswanth/Dice-Game/main/images/dice" + randomNumber2 + ".png";

document.querySelectorAll("img")[1].setAttribute("src", randomImageSource2);

if (randomNumber1 > randomNumber2) {
  document.querySelector("h1").innerHTML = "Player 1 Wins!";
} else if (randomNumber2 > randomNumber1) {
  document.querySelector("h1").innerHTML = "Player 2 Wins!";
} else {
  document.querySelector("h1").innerHTML = "Draw!";
}
.container {
  width: 70%;
  margin: auto;
  text-align: center;
}

...
<!DOCTYPE html>
...

</html>

If you have any suggestions on how to reload only a specific div (changing only the dice images) without refreshing the entire page, please let me know. Thanks In Advance.😊

Answer â„–1

If you need to refresh a specific section of your webpage, you can utilize the power of jQuery's load function. Here is an example of how you could implement it:

function refreshSection()
{ 
    $( "#sectionId" ).load(window.location.href + " #sectionId" );
}

Important: Make sure to replace "sectionId" with the actual ID of the div you want to reload.

Remember to include the space before the "#" in the load function selector: + " #here" You can trigger this function at set intervals or bind it to a click event.

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

Navigate to the website and display it within the central frame, utilizing the bootstrap vertical tabs layout

I recently obtained a bootstrap template that features vertical tabs along with a main frame. Check out the current view of the webpage here My goal is to have the ability to click on 'Google' (the link on the left) and have the Google page loa ...

PHP does not display the Ajax data from JavaScript

I'm having trouble outputting the elements of an array in PHP that was passed from JavaScript. The JavaScript array seems to be passed correctly, but it's not printing via PHP for some reason. Apologies if my code isn't properly indented. Th ...

What is the method to create a number using a textbox through javascript?

Whenever a number is entered into the main_textbox, a JavaScript function is called on the onblur event of the textbox using the following code: function generate_bale(){ var bale=document.getElementById("number_of_bale").value; var boxes = "< ...

What is the reason behind webpack attempting to interpret my readme.md files?

When using Webpack, I encountered the errors below. Despite the build appearing to be successful, I am still puzzled as to why these errors are occurring. WARNING in ./{snip}/readme.md Module parse failed: C:{snip}\readme.md Unexpected token (1:7) Y ...

Communication-Friendly-Component properties not properly handed over

I have integrated the react-chat-widget into my application and I am attempting to invoke a function in the base class from a custom component that is rendered by the renderCustomComponent method of the widget. Below is the code for the base class: impor ...

The Owl Carousel arrows and dots are perfectly aligned at the same level

When using the owl carousel, I encountered an issue where the arrows and dots are aligned at the same height. I would like to have the arrows centered vertically while keeping the dots at the bottom as they are currently positioned. However, whenever I att ...

How can I prevent mouse movement hover effects from activating in a media query?

I have implemented a custom image hover effect for links in my text using mousemove. However, I want to disable this feature when a specific media query is reached and have the images simply serve as clickable links without the hover effect. I attempted ...

Divide Angular ngFor into separate divs

Here is an example of my current array: [a, b, c, d, e, f, g, h, i] I am aiming to iterate through it using ngFor and split it into groups of 3 elements. The desired output should look like this: <div class="wrapper"> <div class="main"> ...

Utilizing Lodash's uniqWith function, it allows us to specify how we want to

Currently, I am utilizing the lodash uniqWith method to eliminate duplicate items with the same id from my array. However, the lodash method retains the first duplicated item, whereas I actually want to keep the last duplicated item. Is there a way to ac ...

Is there a way to improve this code without the need for the variable `a`?

Assist in enhancing the performance of the functions below, without using the variable a getFieldsGroups(){ let list = []; if(this.activeTab.fieldsGroupName === ''){ this.activeTab = { groupIndex: 0, subgroupIndex: 0, f ...

Add and attach an event handler to dynamically created elements

I am facing an issue where the second element generated after clicking the button does not interact with the event handler, despite the browser indicating that it's an event. https://i.sstatic.net/ztT1l.png Here is the code structure with the event ...

Requesting an image from the live website using a GET method

Recently, I registered a domain for my personal website. While the site is still a work in progress, I noticed that the logo image fails to display when viewed through the browser on the site. Surprisingly, it shows up perfectly fine when accessed via loca ...

Enhance the look of the dropdown menu

Seeking advice on enhancing the appearance of a dropdown menu in an ASP.NET core app using Bootstrap CSS. Suggestions for improving spacing and text aesthetics would be appreciated. Thank you, Peter https://i.sstatic.net/HA5dZ.png Below is the code snipp ...

Dynamic grid alignment: lock to edge of screen

Currently, I am in the process of creating an animated grid that adjusts its size when hovered over. To view the code pen project, please ensure you are in desktop mode rather than mobile: https://codepen.io/marco_lu/pen/abBmwEJ The JavaScript code utili ...

Utilizing AngularJS and CSS selectors for effective form validation

Just a quick question: Can someone explain why the summary class is able to be directly added to the <span> element, but not the ng-classes onto the <form> tag? Could it be because the ng-classes are dynamically generated at runtime? What rul ...

The Alphavantage was acting strangely when I ran a Google script

After following a tutorial video on YouTube, I was confident that my Google script for Google Sheets was working perfectly. However, I encountered two strange issues that I just can't seem to figure out. The code below is exactly what I need - it ...

The presence of whitespace is causing disruptions in the text alignment within div elements

I've noticed some unwanted white space on my website when viewing it in Chrome and Internet Explorer. It's interrupting the flow of text and I can't figure out where it's coming from. Can someone help me locate and remove this pesky wh ...

Unable to load additional posts at this time

Currently, I am working on implementing a LoadMore function that will add more posts based on the number of posts currently displayed and the total number of posts in the DOM. However, I am facing an issue where, upon console logging the listofposts and ...

What is the method for converting JSON to a JavaScript object?

I am currently utilizing asp.net C#. Here is the code snippet I have: CalendarInfo oInfo = new CalendarInfo { title = "Joe Bloggs", start = System.DateTime.Now.ToString("yyyy-MM-dd"), end = System.DateTime.Now.AddDays(3).ToString("yyyy-MM-dd") }; var s ...

Cloning blank records in SQL during data refreshment

My recent project involves a system where users can input data that gets stored in a MySql database via SQL queries. However, I'm encountering an issue where empty data is inserted into the database every time the page is refreshed. The connection, qu ...