The div functions seem to stop working properly after they have been multiplied

Initially, I use JavaScript to multiply the div but then encounter issues with the function not working properly.

function setDoorCount(count) {
    $('.doors').html('');
    for (var i = 0; i < count; i++) {
        var content = "<div class='door' data-door='" + i + "'><img id='door-id-" + i + "' src='' /></div>";
        $('.doors').append(content);
    }
}

Subsequently, when attempting to open a dialog, it does not behave as expected.

var selectedDoorId = 0; 
var openDialog = function() {
    selectedDoorId = $(this).attr('data-door');
    $('.dialog-button').click( function() {
        var color = $(this).attr('id');
        document.getElementById('door-id-' + selectedDoorId).src = 'http://www.#.asd' + color + '.png';
        $('#door-panel-color').dialog('close');
    });
    $('#door-panel-color').dialog('open');
}
$(function() {
    $( "#door-panel-color").dialog({
        autoOpen: false,
        minHeight: 'auto',
        width: 650,
        modal: true,
        open: function(){
            $('.ui-widget-overlay').bind('click',function(){
                $('#door-panel-color').dialog('close');
            })
        }
    })
    $('.door').click(openDialog);
});

HTML:

<div class="doors">
    <div class="door" data-door="0">
        <img class="door-color" id="door-id-0" src="http://liukuovitukku.fi/images/door-colors/pahkina.png" />
    </div>
</div>

Answer №1

To improve the functionality, you should implement the following changes in your code:

  function validateDoorCount(count) {
            if (parseInt(count.value) !== 0) { //Update made here
                if (count.value !== 0) {
                    updateDoorCount(count.value);
                }
            } else {
                document.getElementById('door-count').innerHTML = '<font color="#F00">cannot be a letter</font>';
            }
        }
        function updateDoorCount(count) {
            alert(count)
            $('.doors').html('');
            for (var i = 0; i < count; i++) {
                var content = "<div class='door' data-door='" + i + "'><img id='door-id-" + i + "' src='#'/></div>";
                $('.doors').append(content);
            }
            $('.door').click(openDialog); //Added this line for better performance
        }

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

Running Handlebars using NodeJS can sometimes result in a "Cannot find module './parser'" error

After successfully creating and implementing a Handlebars template in the Browser, my next goal is to utilize the Handlebars precompiler, which requires a NodeJS module. I have already downloaded Handlebars for NodeJS along with all dependencies locally (n ...

Issue with displaying current date and time dynamically (JavaScript/JQuery)

In order to create a real-time chat using websockets, I want to show the time of a message posted in this format: "% sec/min/hour/day now". After conducting some research, I came across two functions that I have modified : // Get the TimeStamp va ...

Prevent the occurrence of the dreaded 'undefined property' error when attempting to validate a grandchild property

I need to set the state of a React component to the location.state passed with React Router. If it doesn't exist, I want to set it to 0. However, in some cases, the parent (location.state) of the property I am checking for (currentItem) doesn't ...

Pressing a button to input a decimal in a calculator

I am encountering an issue with inputting decimals in my JavaScript. I have a function that checks if the output is Not a Number (NaN). If it is, I want it to output a decimal instead. I attempted to add another conditional statement like this: var opera ...

The AJAX response shows a value of "undefined"

My HTML page contains these codes, which display a list of employees from the database. <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <script src="Scripts/jquery-1.10.2.js"></script> ...

Is it feasible to determine the specific memory address of an Object in a program?

Is it possible in JavaScript to determine the memory location and memory usage of an object? Any assistance on this matter would be greatly appreciated. ...

Browse through and review all information - utilizing python selenium

I am currently working on a project that involves scrolling and retrieving every post made by users. However, I am facing an issue where my code is only reading 2 or 3 posts before moving on to the next one. I have tried adjusting the sleep() duration, p ...

How AngularFire automatically adds back a removed item in a Firebase array

I have been working on a way to remove an item from my $firebaseArray called "boxes". Here is the "remove" function: function remove(boxJson) { return boxes.$remove(boxJson); } Although it gets removed, I noticed that it immediately reappea ...

Is there a way to include an edit, delete, details, and create new icon in an @html.actionLink within MVC?

This HTML code is specific to MVC and represents the formatting used: 1. @*@Html.ActionLink("Delete", "Delete", new { id=item.ActivityDepreciationTypeId })*@ 2. @*@Html.ActionLink("Details", "Details", new { id=item.ActivityDepreciationTypeId })*@ ...

Issues with a responsive website not displaying correctly in the Firefox browser

I have created a carousel that is functioning correctly. However, I am encountering an issue with making it fully responsive. It appears to work properly in webkit browsers, but there are some issues in Firefox. Despite the images resizing properly, the ...

Ways to expand the border horizontally using CSS animation from the middle

Currently, I am experimenting with CSS animation and I have a query regarding creating a vertical line that automatically grows in length when the page is loaded. I am interested in making the vertical line expand from the center in both upward and downwar ...

Have I potentially compromised my project by executing the command `npm audit fix --force`?

While working on a React project using Vite, everything was going smoothly. I decided to incorporate some charts and came across the recharts package, which I found quite appealing. So, I added it to my project using the command npm i recharts. After runn ...

Exploring the integration of Bootstrap Confirmation into an ASP.NET web page

I'm currently working on implementing Bootstrap Confirmation (http://ethaizone.github.io/Bootstrap-Confirmation/#top) on an ASP.NET web page using C#, but I've encountered some issues. Master Page References <asp:ScriptReference Path="Script ...

Altering CSS attribute values using a random number generator

Is there a way to randomly change the animation-duration attribute in the following CSS code? I want it to range from 0 to 1. @keyframes blink { 50% { border-color: #ff0000; } } p{ animation-name: blink ; animation-duration: 0.1s ; animatio ...

Personalize JSON Reporter to Display Captured Information - WebdriverIO

I'm currently in the process of scraping data from an android app using WDIO and APPIUM. After successfully storing the scraped data in an array, I now aim to automatically output this array data to a .json file, rather than manually copying and pasti ...

The contents within the div element exceed the width of its parent element

Hey there, I'm currently working on a Rails application to showcase some links in a horizontal layout. The links are indeed displaying horizontally without wrapping, just as intended. However, I've noticed that the containing div is wider than th ...

Show on Screen as Label Text

I am looking to change the display format from a popup box to a label text. How can I achieve this using JQuery? JQuery <script type="text/javascript> function ShowCurrentTime() { PageMethods.GetCurrentTime(document.getElementById("<%=txtUserNam ...

Comparison of button text alignment: stacking vs inline placement

I currently have a button on my webpage labeled "Learn More." Unfortunately, the text on the button is not lining up as it should. Instead of flowing in a line, the text seems to be stacking on top of each other. The HTML code for the button is included ...

How to use jQuery to add an unordered list between two elements

Here is the current structure I am working with: <ul> <li id="first">Fist Item</li> <li id="second">Second Item</li> <li id="third">Third Item</li> .................. </ul> My goal is to add ...

How to create a sequence of queries to a mongoDB database (using mongoose) depending on a condition

Source Code let placeForSearch="hampi" let filteredHotelFacilities=req.body.filter //["Free Wifi"] let hotels = await Hotel.find({placeForSearch}) .where("facilities") .select(selectedProperties) .skip(pageNu ...