I am having trouble modifying the content of a div using Jquery append

I have a plan to use jQuery to change images. I have multiple image files named choose 01.png, choose 02.png, and so on. When an image is clicked, I want it to be replaced with the corresponding choose 01.png file. Once 5 images have been clicked and changed, I intend to remove them from the table and update the content inside the order id with new <p>. The image changes are purely cosmetic.

Below is my attempt at using jQuery to achieve this, but it doesn't seem to work as expected:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Magic</title>
    <link rel="stylesheet" href="magic.css">
</head>
<body>
    <div id="board">
        <div id="orders"></div>
        <table>
            <tr>
                <td><img id="00" class="card" src="images/number 00.png"  alt="0"></td>
                ...
                <td><img id="09" class="card" src="images/number 09.png" alt="9"></td>
            </tr>
        </table>
    </div>
    <script src="../jquery-3.5.1.js"></script>
    <script src="script.js"></script>
</body>
</html>

Javascript code using jQuery:

$(document).ready(function() {
    $("#orders").append("Click 5 numbers you like.");
    var choose = 0;
    $(".card").click(function() {
        var $id = $(this).attr("id");
        $(this).attr("src", "images/choose"+$id+".png"); //Change the image when clicked
        ++choose;
        
        if (choose == 5) {
            $("#orders p").remove();
            var display = "pfftt";
            $("#orders").append(display);
        }
    });
});

Am I missing something in my code?

Answer №1


$(document).ready(function() {
    $("#orders").append("Select five numbers that appeal to you");
    var selectedNumbers = 0;
    $(".card").click(function() {
        var $id = $(this).attr("id");
        $(this).attr("src", "images/choose"+$id+".png");
        ++selectedNumbers;

        if (selectedNumbers == 5) {
             $("#orders p").remove();
             var displayMessage = "You have made your selection";
             $("#orders").append(displayMessage);
        }

    });
});

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

ERROR UnhandledTypeError: Unable to access attributes of null (attempting to retrieve 'pipe')

When I include "{ observe: 'response' }" in my request, why do I encounter an error (ERROR TypeError: Cannot read properties of undefined (reading 'pipe'))? This is to retrieve all headers. let answer = this.http.post<ResponseLog ...

Enhance user experience by implementing an interactive feature that displays

I have a form for adding recipes, where there is an ingredients button. Each recipe can have multiple ingredients. When the button is clicked, an input field for adding ingredients should appear below the ingredient button. What I've attempted so far ...

Packaging an Angular project without the need for compiling

In order to enhance reusability, I structured my Angular 6 Project into multiple modules. These modules have a reference to a ui module that contains all the style sheets (SASS) as values such as: $primary-text-color: #dde7ff !default; Although this app ...

Is it possible to showcase special characters retrieved from a MYSQL database using a modal form implemented in J

I am working on a jQuery code that retrieves data from MySQL and displays it in a modal window form. editRow: function(row){ var values = row.val(); ... $editor.find('#mail_username').val(values.mail_username); ... $modal.data('row', r ...

Validating the existence of a file through HTTPS in Javascript

After attempting to retrieve the file using this code, I've encountered an issue - it works with HTTP requests but not HTTPS requests (which is what I require). function checkUrl(url) { var request = new XMLHttpRequest(); try { request.ope ...

What is the best way to receive the result of an asynchronous function as an input and showcase it using express?

I have recently started learning about node and express My goal is to create an API that can fetch data from an external source and use that data for my application Is there a way to retrieve the result of an asynchronous method and display it using the ...

Guide on adjusting shadow intensity with a Directional Light on a BoxGeometry in Three.js

I've been working on adjusting the light and shadow settings for Box Geometry. Specifically, I am using a Directional light with a certain intensity level. However, I've noticed that when I decrease the intensity, the plane appears darker while t ...

Having trouble getting Google to show breadcrumbs in search results

I've been trying to implement schema.org's breadcrumb microdata in order to have Google display my breadcrumbs in search results. However, when I check with Google's structured data testing tool, the breadcrumb is not showing in the search r ...

How can I choose a user based on the department selected in a dropdown list in CakePHP using the onchange event

My goal is to create a dynamic view that links two tables, 'user' and 'department'. The 'department' table has fields for id and name. I want to implement a feature where selecting a department name from a dropdown list trigge ...

What is the best way to retrieve the scope of ng-repeat from another directive located on the same element as ng-repeat?

Is it possible to access a property from the ng-repeat scope in another directive within the same element as the ng-repeat directive? For instance, I would like to be able to use the child.class property in this scenario: <div ng-class="{{ child.class ...

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 ...

My initial junior UI/UX assignment: Can you confirm whether this form modal dialog is pixel-perfect, and offer any suggestions for improvements

Currently diving into my first project as a Junior UX/UI Designer. Coming from a background in software engineering, I decided to challenge myself by focusing on design. I'm seeking feedback on the pixel perfection of this modal window, which my seni ...

After mapping the elements of the array twice, generate a new array

Two differently formatted bits of data may be received, each requiring different character stripping methods. The variable names are temporary and will be changed once the function is operational. const cut = flatten.map(obj => { return obj.file. ...

Enhance your Django website with advanced autocomplete functionality using the powerful django-ajax-selects

I require assistance in integrating auto-complete fields into my Django Project. My goal is to retrieve a list of relevant items from the database as the user inputs their query. For this task, I am utilizing the django-ajax-selects package. The project ...

Presenting data in various formats

I've got a JSON file with a list of data that includes months and years. I want to display all the months, but have the year only appear once in a big block area. Here's an image example: https://i.stack.imgur.com/Ps3OF.png The image demonstrates ...

Is it possible for JavaScript code to access a file input from the terminal?

When I run the command cat input.txt | node prog.js >result.txt in my terminal, I need to use an input file. This is my code: var fs = require('fs'); var str = fs.readFileSync('input.txt', 'utf8'); str.replace(/^\s* ...

Angular 5 Service Unit Testing for UPDATE Function

Currently, I am implementing a stepper feature with back, step, next steps. On the last step, when the user clicks 'done,' I need to call a service to update user data. While I have successfully tested the backStep() and nextStep() methods, I now ...

The presence of an additional bracket is being triggered by the JSON array following

Currently, I am developing a function in Freemarker to iterate through all selected checkboxes and then display the same response on the form itself. The form is structured in Freemarker format, and I am utilizing JavaScript to create separate arrays for e ...

Calculating collision between circles in Three.js

I've been working on incorporating circle to circle collision for a pool game, aiming to make the balls bounce apart correctly upon impact. Despite trying various tutorials and scouring multiple questions on stackoverflow, I haven't found a solut ...

Closing tags for an incomplete HTML snippet

Recently, I came into possession of a website with a news section that showcases a summary of each news article. Oddly enough, the original creators thought it would be sufficient to display only the first X characters of each article. This decision quickl ...