Tips for styling an inline list using CSS before revealing it with jQuery's .show method:

In my project, I want to display a row of 'cells' which are simply images of black or white squares based on their ID. The issue I am facing is that when the button is clicked, the row list is shown without the CSS applied, and then after a brief delay, the CSS is applied and it displays correctly. Is there a way to reverse this process so that the CSS is applied first and then the table is shown to the user?

CSS

    ol, li { display: inline; margin: 0 0 0 0; padding: 0 0 0 0; list-style: none; }
    img { margin: 0 0 0 0; padding: 0 0 0 0; }

Jquery

            $('#generateTbl').hide();
        $('#generateBtn').click(function () {
                showCellConfiguration();
        });

        var showCellConfiguration = function () {
            loadCells($('#numOfCells').val());
            $('#generateTbl').show('slow', function () { });
        }

        var loadCells = function (numOfCells) {
            for (var i = 0; i < numOfCells; i++) {
                $("#row").append('<li><img id="cellOff" class="buttons" src="off.jpg" /></li>');
            }
        }

HTML

<table id='generateTbl'>
    <tr><td><ol id="row"></ol></td></tr>
</table>

Answer №1

function createCells(numOfCells) {
            for (var i = 0; i < numOfCells; i++) {
                var cell = $('<li><img id="cellOff" class="buttons" src="off.jpg" /></li>');
                cell.ready(function() {
                    $("#row").append($(this));
                });

            }
        }

let's see if this function works

Answer №2

One possible solution, albeit slightly unconventional, is to manipulate the position of the new DOM element off-screen temporarily before inserting it:

var preloadItems = function (quantity) {
    for (var i = 0; i < quantity; i++) {
        var newItem = $('<li><img id="itemOffScreen" class="buttons" src="off.jpg" /></li>');

        // Position the element off-screen
        newItem.css({ 
            position: absolute,
            left: -9999
        });

        $("#container").append(newItem);

        // Move the element back onto the screen after 
        newItem.css({
           position: auto
        });
    }
}

To enhance this approach, consider adding a delay before moving the element back into view:

$("#container").append(newItem);

// Wait for 200ms before repositioning the element   
window.setTimeout(function() {
    newItem.css({
        position: auto
    });
}, 200);

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

What's the significance of the #/ symbol in a URL?

Currently, I am developing Ruby on Rails web applications. The URL of my webpage appears as follows: http://dev.ibiza.jp:3000/facebook/report?advertiser_id=2102#/dashboard From this link, I have identified that the advertiser_id is 2102 but I am unsure a ...

Steps to update the action attribute in a form element upon clicking a button

Within the following code snippet, there are 3 buttons - update, back, and delete. Depending on which button is clicked, the $page variable should be assigned to the action attribute in the form element, with the posted data being sent accordingly. <f ...

Some devices experiencing website update issues while others are successful

My website is functioning properly on my laptop and phone, but not on my school iPad (managed by the district). I have already cleared the cache, but the site does not update. It was working fine earlier, and I need to present it next Monday. Please help a ...

Operating my application on a 2G network

Overview of my mobile application: I have created a simple HTML/CSS/JavaScript-jQuery app using PHONEGAP. The app utilizes a WCFService on a server for database communication, which is accessed through AJAX calls. After building and testing the applicati ...

Difficulty arises when trying to extract specific information from an ajax response using the jQuery.filter

The code snippet below seems to be causing some trouble. It's supposed to filter HTML content that includes a div with the class "filtered_entries_box", but it's not working as expected. $.ajax({ "url" : "start.php", "type" : "POST", ...

Creating a dynamic webpage where multiple images are displayed on a single canvas using

I'm currently working on a chess project where I'm using a canvas to create the game board and draw 32 chess pieces on it. To update the screen when a piece moves, I've implemented requestAnimFrame. However, I'm facing an issue where th ...

Leveraging Django's JSONResponseMixin for handling AJAX responses

I'm currently using Django in conjunction with AJAX. My goal is to have a javascript file (vote.js) POST data to a Django View, which will then respond with JSON data to be utilized by my javascript's callback function in the HTML. Below is the ...

How can I modify the style of a cell in jqgrid when the mouse hovers over it?

I need to change the class of cells in the first column of my grid. Currently, I set them using this code: $("#myGrid").jqGrid('setCell',rowid,'column_1', '', '**ui-state-default**'); Is there a way to modify the c ...

Breaking down a jQuery array into separate JSON objects

Hi there, I am currently working on constructing a post request. Here is the format I need to send: const data = { categories: [ { id: 9 }, { id: 14 } ], } However, what I have is ["29", "23", "22&quo ...

Need to return false even though Jquery form check currently returns true

Upon form submission, the following check is executed: if((formData[4].value == 1 || formData[4].value == 2) && !formData[2].value) { alert('Please fill out the key field'); return false; } else { $.ajax({ url: "/aja ...

Thymeleaf is responsible for fetching the static resources by referencing the REST URI

I am currently utilizing thymeleaf as my chosen template engine for a spring boot project. Here is the directory structure: src/main/ |- java | - UserAdministrationController.java |- resources | - static | - admin | - css ...

Guidelines for transmitting form information to a web API using Angular

I am currently working on an Angular 6 project where I have a form and a table that retrieves data from a web API. I want to know if it's possible to send the form data to that web API. Here is the code snippet that I have so far: HTML Form: &l ...

Is there a way to determine if jQuery Mobile has already been loaded?

I am dynamically loading jqm and I'm trying to determine if it has been previously loaded in certain scenarios. Is there a method to check for the presence of jqm? ...

Sending data using jQuery to a web API

One thing on my mind: 1. Is it necessary for the names to match when transmitting data from client to my webapi controller? In case my model is structured like this: public class Donation { public string DonorType { get; set; } //etc } But the f ...

Utilizing JQuery to extract the image title and render it as HTML code

I am currently facing an issue with displaying an image in my project. I want to hide the image using CSS (display: none) and instead, retrieve the value of its title attribute and display it within the parent div by utilizing JQuery. Despite knowing tha ...

Rotating SVG images with ease at any center point

I attempted to remove the unsupported animateTransform element: <animateTransform attributeType="xml" attributeName="transform" type="rotate" from="0 20 20" to="360 20 20" dur="1.2s" repeatCount="indefinite" /> and replaced it with CS ...

Error occurred while retrieving JSON array using Jquery

var groupMembers = $.parseJSON(members); $.each(groupMembers, function (index, item) { $.ajax({ type: "POST", url: "'.base_url(" / panel / listNotifications ").'/'.$id.'/" + valueSelected, d ...

developing both vertical and horizontal 'cross out'

I'm attempting to add a 'spacer' between buttons on my website using the word "or" with a vertical line centered above and below it. I've tried HTML <div class="footer-btn-wrap"> <div class="decor"><a href="... < ...

Is there a way to resize SVG images without having to modify the underlying source code?

Within my Vue Single File Component, there is a prop labeled svg, which holds a string of SVG markup like <svg>...</svg>. What is the best way to render and resize this SVG content? ...

Is it necessary to have the WAI-ARIA attribute aria-labelledby on a nested input element?

Do I need the attribute aria-labelledby on an <input> element nested within a <label> element that acts as a label for the <input>? For example: <label>This is an input field: <input type="text" /> </label> Or should ...