Using JavaScript to modify the width of an element

I have been struggling to increase the width of an element in my HTML using JS, but it's not working correctly.
This is crucial for responsive design as the content should scroll when necessary.
I attempted to achieve this with the following code, but unfortunately, it did not produce the desired outcome. In responsive design, everything seems mixed up.

<script>
    $(document).ready(function (e) {

        $('.seat-ready').click(function (e) {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active');
            } else {
                $(this).addClass('active');
            }

        });
    });
    setTimeout(function () {
        // Code to calculate and set the width dynamically
        $('.chairs .row').each(function () {
            // Width calculation based on child elements
        });

        // Additional styling for specific rows
        $('.chairs').each(function () {        
            // Styling logic specific to certain rows
        });
    }, 500);
</script>

Answer №1

Allow me to provide a suggestion based on your inquiry. Perhaps you are seeking something similar to this example.

Please note: The JavaScript code is primarily used for generating the HTML, which closely resembles what you have implemented.

var HALL_DATA = [
/* 0=offset, 1=ready, 2=sold, 3=disabled */
[1,1,1,1,1,1,1,1,0,0], //1
[1,1,1,1,1,1,1,1,1,0], //2
[1,2,2,2,2,2,1,1,0,0], //3
[1,1,1,1,3,3,3,3,3,0], //4
[1,1,1,3,3,3,3,3,0,0], //5
[1,1,1,1,1,1,1,1,1,0], //6
[1,1,1,1,1,1,1,1,0,0]  //7

];
var SEAT_CLASSES = ['offset', 'seat-ready', 'seat-sold', 'seat-disabled'];
var seatTemplate = '<a href="#" class="{SEAT_CLASS}">{SEAT_NR}</a>';
var rowTemplate = '<div id="row-{ROW_NR}" class="row">{ROW_CONTENT}</div>';

function getHallContent() {
var html = '';
for(i=0; i<HALL_DATA.length; i++) {
html += rowTemplate.replace('{ROW_CONTENT}',getRowContent(i)).replace('{ROW_NR}',i+1);
console.log()
}
return html;
}
function getRowContent(theIndex) {
rowHtml = '';
rowData = HALL_DATA[theIndex];
for(j=0; j<rowData.length; j++) {
var seatClass = SEAT_CLASSES[rowData[j]],
seatNr = j+1; //cause j is index
rowHtml += seatTemplate.replace('{SEAT_CLASS}',seatClass).replace('{SEAT_NR}',seatNr);
}
return rowHtml;
}

$(document).ready(function() {
$('.hall-wrap').html(getHallContent());        
});
.hall-wrap {
    display:table;
    width:100%;
    max-width:200px;
}
.row {
    display:table-row;
}
.row a {
    display:table-cell;
    color:#333;
    text-decoration:none;
    width:20px;
    height:20px;
    font-family:sans-serif;
    text-align:center;
    line-height:20px;
}
.seat-ready {
    background: #89CC45;
}
.seat-sold {
    background: #C93E41;
}
.seat-disabled {
    background: #C2C5CF;
}
a.offset {
    background:transparent;
    color:transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hall-wrap"></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

Tips for compiling Bootstrap SCSS to specifically include only the extended classes

Is there a way to compile my custom bootstrap SCSS file into a CSS file that only includes the classes I created in my custom file? For example, I want to eliminate the extra CSS classes added to my HTML code. Instead of including unnecessary classes lik ...

Background color vanishes (occasionally) on the contact section of my website when viewed in Chrome

My contact page has an issue that only occurs in Chrome, where the background disappears most of the time. This problem is not present in Firefox, Edge, or Safari on my TA's MacBook, but interestingly, hovering over a box resolves the issue on her dev ...

Efficient Techniques for Deleting Rows in a Dynamic JavaScript Table

I'm facing an issue where I want to remove each line added by the user, one by one. However, my current program is removing all the rows from the table instead of just one at a time. The objective is to allow the user to remove a specific row if they ...

managing numerous outdated requests on the server

Within my application, there is a map feature that sends a request to the server whenever a user interacts with it, such as zooming in or panning. The issue arises when users perform rapid actions, leading to multiple requests being sent to the server and ...

Having trouble connecting angular repository data with promise

I have been trying to implement the repository pattern in my Angular application. I can see that the JSON data is successfully retrieved over the network when I call my repository, but I am facing issues with binding it to my view. I have injected the rep ...

Adjust the font size in CSS based on a specified range of values

When the screen size is small (mobile), the font size is fixed at 14px. On really large screens, the font size is set to 18px, ensuring it never goes below 14px or above 18px>. This is achieved using media queries.</p> <p>However, for the i ...

Issue with horizontal scrolling in ng-scrollbars occurs when scrolling from right to left

We are currently developing a single page application that supports two languages, one being right to left and the other left to right. For scrolling functionality, we have implemented ng-scrollbars, an Angularjs wrapper for the malihu-custom-scrollbar-pl ...

What is the best way to pass only the second parameter to a function in TypeScript?

Let's consider a TypeScript function as shown below: openMultipleAddFormModal(commission?: Commission, people?: People): void { // some data } To make a parameter optional, I have added the Optional Chaining operator. Now, how can I modify the code ...

Use the Segoe UI typeface on Internet Explorer for a sleek

I have implemented a custom Segoe UI font in my CSS file. While it displays correctly in Chrome, I am facing an issue where IE and Firefox are showing the default font in certain areas. Below is the snippet of the CSS code: body { margin:0px auto; ...

Chrome may clip the gradient radius when setting the focal point outside of the SVG gradient

I just joined this community and I might have some questions that could be considered too basic. Recently, I've been trying to understand SVG and came across something that left me puzzled. Take a look at this: <svg xmlns="http://www.w3.org/20 ...

Share an image using only the publish_actions authorization

There are some apps I've come across that only request access to users_photos and publish_actions (not publish_stream or photo_upload). Surprisingly, they are able to upload photos without being approved for User Generated Photos (e.g. Instagram). Wh ...

Center-aligning images in Bootstrap Carousel

I've implemented the Bootstrap 4 Carousel on my website and it's functioning properly. However, I'm facing an issue where I need to align the slider image in the center of the carousel. Currently, the image is aligned to the left side, caus ...

The URL's ajax GET request is timing out despite it working fine in browsers and CURL

I came across a question similar to mine, but unfortunately it does not have an accepted answer. My issue lies with the ajax request timing out. Strangely enough, when I make a `GET` request using the browser or `curl` on the same URL, everything works pe ...

Enhanced button effect: image shaded on hover

Can someone help me figure out how to make the button and image darker when hovered over? I tried something but it didn't work... Here is a demonstration: http://jsfiddle.net/h2o8w5y6/ <!--- example code snippet --> <div class="col-lg-4 ...

Discover the process of transitioning your animations from Angular to CSS

I have successfully implemented a fade-in/out animation using @angular/animation, but now I am looking to transfer this animation to CSS and eliminate the dependency on @angular/animation. Here is my current animation setup (triggering it with [@fadeInOut ...

Blog Writer: Picture quality compromised when adjusting size

Issue with blurry images after resizing. The problem is noticeable in the three main images located under the Header section. To preview the blog, click on this link: Click Here Your assistance in solving this issue would be greatly appreciated. Thank yo ...

Fast JSON retrieval

I am attempting to implement an instant search feature using json. Despite my efforts, the code does not seem to be functioning properly. Can someone kindly review it and assist me in resolving the issue? Thank you in advance. http://jsfiddle.net/hMyr7/ ...

Prepare for the possibility of being labeled a failure, even if you have already been labeled

I wrote a test case code using Jest to check the functionality of my application. let ticketsTransformer = new TicketTransformer(); ticketsTransformer.transform = jest.fn(() => results); expect(ticketsTransformer.transform).toHaveBeenCalled(); con ...

Add a label to the location of an event

I have an unordered list like this <ul class="fine"> <li>One</li> <li>Two</li> <li>Three</li> </ul> When I click on a li item, I want to dynamically add a <textarea> under that specific li element, as ...

How to Use Vue.js to Find the Nearest Div Element with a Specific

Below is the HTML code I am working with: <div id="app"> <div class="image"> <div class="overlay"> <p>Some overlay text</p> </div> <img src="https://placeimg.com/640/480/any" class="img-fluid"> ...