My JavaScript for loop is malfunctioning

Having trouble with the SelectSeat function, it doesn't seem to be working even though I've called it from the HTML onclick event.

<html>

<head><link rel="stylesheet" type="text/css" href="mandigo.css"> 
<center> <b><i><font size="25" color="Blue"> Ticket Booking </b></i>  </font>
<script>

    var seat = [true,false,true,false,false,false];

    var sel_seat=-1;//for selecting a seat

    function setHtml(){

        alert("check");
        for(var i=0;i<seat.length ;i++){
        if(seat[i]){

            document.getElementById("seat"+(i+1)).src ="Avail.png";
            document.getElementById("seat"+(i+1)).alt ="Available";             
        }

        else{

            document.getElementById("seat"+(i+1)).src ="UnAvail.png";
            document.getElementById("seat"+(i+1)).alt ="UnAvailable";               

        }
    }
}

    function SelectSeat(){

        setHtml();

        for(var i=0;i<seat.length;i++){

            if(seat[i]){
                sel_seat=i+1;
                var accept=confirm("Seat "+(i+1)+" is Available. Book it ??");
                if(accept){
                    Booking();// in Booking change the seat color using anothr picture.
                    break;//to break out of the loop
                }
                //if not true loop continues until it fins an empty seat
                /*if(!accept){

                }*/

                if(sel_seat==-1&&i>=seat.length+1)
                    alert("No Seats Are Available");
            }

        }

    }

    function Booking(){

        document.getElementById("seat"+sel_seat).src="SelectedSeat.png";

    }





</script>
  </head>

<body>

<div><pre>

    <img  src="Avail.png" alt="Available" id="seat1"width="90" height="90"><img  src="Avail.png" alt="Available" id="seat2"width="90" height="90">
    <img  src="Avail.png" alt="Available" id="seat4"width="90" height="90"><img  src="Avail.png" alt="Available" id="seat5"width="90" height="90">
    <img  src="Avail.png" alt="Available" id="seat3"width="90" height="90"><img  src="Avail.png" alt="Available" id="seat6"width="90" height="90">
</pre></div>


<center>

    <input type="Button" value="Find Me a Seat" onClick="SelectSeat();"/>

</body>

</html>

Apologies for not providing the full program initially. When I say it's not working, I mean that nothing happens and there are no error messages displayed either.

Answer №1

Remember to invoke the SelectSeat() function for it to work properly.

To activate the function, you can do so by various means such as

automatically when the page loads:

window.onload=function(){SelectSeat()};

or trigger it upon clicking something:

<div onclick="SelectSeat()">click me</div>'
;

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 the Node.js File System to Store Distinct File Names

I am utilizing the Node File System to save uploaded images. My approach involves using a while loop to check for existing file names and incrementing them until a unique file name is found. However, I have encountered issues with my code. I keep receivin ...

Maximize efficiency by utilizing frequently invoked functions to conserve computational resources

Suppose I have a function that takes two parameters, x and y: def calculate(x, y): return sin(x)**3 + y In my scenario, I frequently call this function but only occasionally change the values of x and y. Since the function is computationally intensive ...

Enhance the efficiency of your JavaScript code by minimizing repeated selectors

I've been working on a JavaScript project where I came across the following lines of code: $('#content').on('click', 'input[type=submit]', function(){ $('#content').on('click', 'a.removebutton&a ...

Transform the Asp.net JavaScript jsgantt-improved Gantt chart to be compatible with Blazor

Struggling to implement a Gantt chart in Blazor with razor pages after finding a nice one for Asp.net. Any tips on how to proceed? I've already added jsgantt.js and jsgantt.css to wwwroot and included references in index.html. But now, how do I go a ...

The HTML and JavaScript implementation of the Game of Life is experiencing technical difficulties

I attempted to create my own version of the Game of Life using HTML canvas and JavaScript. With the aid of various online tutorials, I was able to write a piece of code that I am still confident in. However, upon launching the HTML page in the browser and ...

Alter the app's entire background color in React.js with a button click

I am facing an issue with changing the background color of my entire React App when a button is clicked. Currently, I am able to change the color of the button itself but not the background. I have imported the App.css file into my project and I am looking ...

Tips for achieving a background animation similar to the one shown on this page

Check out this link: danielcoding.me/resume/#contact I am interested in this animation: screenshot I tried inspecting element on the page, but couldn't find any background images. How can I create this animation? Is it achieved through JavaScript or ...

Unable to get the jQuery click event handler to function properly

I am currently working on an asp.net web application in VS 2013. The application utilizes nested master pages, with the main master page containing the following code snippet: <!DOCTYPE html> <script src="http://code.jquery.com/jquery-lat ...

The jsPDF library creates PDFs with elements that are stretched and distorted

I am currently exploring how to use Html2Canvas in combination with jsPDF to create a PDF from HTML content. I am following a tutorial that guides me through the process. My goal is to render a basic Hello World h1, and while I do get the PDF with the tex ...

The issue with MaterialUI Select's set value is that it consistently falls outside the expected

I'm currently working on a MaterialUI Select component where I am dynamically handling the value parameter. However, I'm facing an issue where even though I set a valid value from the available options, it always shows as out of range. SelectInp ...

Unable to manipulate Ajax response in Firefox extension

I am in the process of creating an AJAX call that will retrieve a complete HTML page and then extract the necessary values from the response. Despite trying various solutions, I am facing an issue where the code works fine when integrated into another HTML ...

The component is no longer able to locate the imported element when it is being shared

Recently, I imported a component into the shared module in order to use it across 2 different modules. However, upon recompiling the app, an error message appeared stating that the jodit-editor, which is utilized by the shared component, is not recognized ...

Construct object in JavaScript recursively

My current project involves creating a file structure index using nodeJS. I am utilizing the fs.readir function to iterate through files and it is working smoothly. However, I am facing an issue when it comes to descending into the directory structure and ...

Ways to verify a single field within a Zod schema individually rather than the entire schema

I am currently working on a form that consists of multiple inputs, including 'hours' and 'price'. The technology stack I am using is NextJS along with server-side actions. When a user clicks the submit button, here's what happens: ...

Transforming KML overlay into PNG overlay for utilization on Google Maps

I am facing an issue with a large KML file containing numerous polygons. When I try to load it into my Google Maps application using the JS library geoXML3, the JavaScript code times out and stops working. However, this problem does not occur with smaller ...

When one div is hidden, the width of another div will automatically adjust to 100%

Is there a way to make #leftdiv expand to 100% when #rightdiv is hidden, and have both <div>s positioned next to each other when a button is clicked? I have successfully aligned both <div>s next to each other upon button click, but I would lik ...

Generate an animated sequence transitioning smoothly from the left side to the center

I am in need of creating an animation that will cause a menu to appear from the left side of the screen when a button is clicked. The menu should expand to cover 55% of the width of the main page. You can view a demo of this animation on JSFiddle by follo ...

Implement TextGeometry.js within Three.js

I'm curious about the utilization of TextGeometry.js from the extras directory in three.js for adding text to a scene. I came across this file but am unsure how to integrate it properly. If you're familiar with its implementation, any guidance wo ...

Activate fancybox after the AJAX content has finished loading

I have a <div> element with the id <div id="displayinformation"> where I am loading content dynamically using Ajax. Some of the loaded content contains links, and I want to display them in a Fancybox lightbox. I have confirmed that the Fancyb ...

Arranging the index of an array according to a separate array of objects in JavaScript using Vue

I have two arrays of objects that need to be sorted based on the value of a key in the other array. array1: [ { group: 'GROUP1', sort_order: 1, }, { group ...