The width of the inline element is determined prior to the JSON content being loaded

One issue I am facing involves styling an inline element, specifically an <a> element. The code in question is:

<a class="text></a>

.text{
display:inline-block;
background:red;
padding:5px
}

The problem arises when trying to load text into this element using JSON. Ideally, I would like the background of the element to be the same length as the text it holds. However, due to the asynchronous nature of loading HTML and then JSON, the length of the text is not captured correctly. While a fixed width could solve this issue, I prefer a solution that dynamically adjusts based on the text content. Are there any alternatives or solutions available? Thank you!

UPDATE: This issue appears more prominent in Chrome compared to other browsers.

Javascript:

$(document).ready(function() {
var data;
function loadContent(){ 
    $.ajax({
        url: "json/content.json",
        data: "nocache=" + Math.random(),
        type: "GET",
        contentType: "application/json",
        dataType: "json",
        success: function(source){
            data = source;
            showInfo();
        },
        error: function(data){
            alert("Failed to load content");
        }
    }); 

}

loadContent();
    function showInfo(){
        $(".text").html(data[lang]['startpage']['text']);
}
});

Answer №1

There is a possible solution to your issue, although there are no guarantees. You can attempt this by passing the source into the function instead of utilizing the undeclared data variable.

$(document).ready(function() {

function loadContent(){ 
    $.ajax({
        url: "json/content.json",
        data: "nocache=" + Math.random(),
        type: "GET",
        contentType: "application/json",
        dataType: "json",
        success: showInfo,
        error: function(data){
            alert("Failed to load content");
        }
    }); 

}

loadContent();

function showInfo(data){
    $(".text").html(data[lang]['startpage']['text']);
}
});

Update

To have all your divs aligned on the same line, modify #game-nav from inline to inline-block.

#game-nav li {
    display: inline-block;
}

display:inline elements (such as <b> or <span>) wrap, which may not be desired in this case. Using inline-block allows wrapping to a new line without breaking mid-word.

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

Turn off automatic resizing of iframes

I'm currently dealing with a webpage that contains an iframe. The iframe contains quite a bit of data, and every time it loads, its height adjusts to match the content within. Unfortunately, this is causing my page layout to be disrupted. Is there a w ...

Utilize the $(#id).html(content) method to populate the following column with desired content

Here is a snippet of my HTML code: <div class="row margin-top-3"> <div class="col-sm-7"> <h2>NFTs</h2> <div class="table-responsive"> <table class="table table-bordered&qu ...

The file-loader in Webpack is failing to copy the files and update the URL

I am encountering an issue with webpack file loader where it is not outputting image files. This problem arises while also importing HTML files as partials in my Angular app. Below is my webpack configuration for handling images using the file-loader: [w ...

Cease all ongoing ajax requests in jQuery immediately

Encountering an issue: whenever a form is submitted, all active ajax requests fail, leading to the triggering of the error event. Any suggestions on how to halt all active ajax requests in jQuery without causing the error event to be triggered? ...

Can CSS be used to dynamically change the color of an element based on a specific value?

Is it possible to dynamically change the background color of table cells using only CSS based on their values? For example, I am looking to achieve a similar effect as in Excel with conditionally formatted values: https://i.stack.imgur.com/6BsLB.png If ...

Utilization of the DatePicker Widget

I am attempting to incorporate a calendar widget on my website (not as a datepicker attached to an input form field, but as a standalone widget!). I have included the necessary file links in the header, however, the widget is not appearing. Here is the con ...

Card image floating to the right using Bootstrap

I have a set of three horizontal cards, not in a card deck as it's not responsive, and I want to align images in the bottom right corner of each. Bootstrap 4 comes with a class for card-img-left which works perfectly. I've attempted using float-r ...

Utilizing React State to dynamically alter the look of a circle element upon clicking a button component

I'm having trouble adjusting the style of a circle component when it's selected by a button in another component. Both components need to change appearance upon selection, but I am facing issues with state management and feeling a bit overwhelmed ...

Tips for passing an array to a Struts action class using Ajax

Having trouble sending dynamic data from a struts iterator as an array to a struts action class individually via an ajax call. Need help on how to receive this data in the struts action class. Check out the sample code below: Struts Iterations <table ...

Generate a JSON file from a Symfony application

I am attempting to export a JSON file from my controller using AJAX, but it seems that nothing is happening. Can anyone point out where I may have gone wrong? Below is my AJAX code: $.ajax({ url: '{{ path('export_index', {'type ...

Is there a way to prevent the Sticky Footer from obscuring the content?

How can I prevent a "sticky" footer from overlapping the content on certain pages while still maintaining its stickiness? I attempted to use min-height: on HTML and body, but it did not resolve the issue. CSS: html { background: black url(images/bg2 ...

How can I use jQuery to automatically redirect to a different HTML page after updating a dynamic label?

When I click on a button in first.html, a function is called to update labels in second.html. However, despite being able to see second.html, I am unable to see the updated values in the labels. Can someone please advise me on how to achieve this? functi ...

Sending a multi-dimensional array to the server using PHP via POST

Similar Question: multi-dimensional array post from form I am looking to transfer data from the client to the PHP server using jQuery's $.post() method. Once the data reaches the server, I want the $_POST['myList'] variable to be in one ...

jQuery's AJAX functionality may not always register a successful response

Below is the code snippet I am currently working with: $(".likeBack").on("click", function(){ var user = $(this).attr("user"); var theLikeBack = $(this).closest(".name-area").find(".theLikeBack"); $.a ...

The tooltip is obscured by the table row

I'm having trouble with a simple CSS fix and can't seem to figure out why. The z-index and overflow visibility properties are not working as expected. I just need 'In Progress' to be displayed above everything else. Some things I' ...

Formatting the numbers for an unordered list located outside of the content

Can the numbers of an ordered list be formatted using the list-position-style set to outside? I am looking for a solution using only CSS since I cannot edit the HTML. Essentially, I want styled numbers with round background to replace the outside numbers. ...

Is there a way to extract all nested URLs on a webpage using rvest and xml2?

I need assistance extracting nested links from the website provided. Despite my efforts, the code I am using is only producing an empty character vector. url1 <- "https://samplewebsite.com/sample-page" url1 <- read_html(url1) url1_body < ...

Express JS is experiencing difficulties with the functionality of the iframe

When I'm using iframe in HTML, it works perfectly fine: <iframe src="main.html" height="100px"></iframe> But when I use Iframe in Express, it shows an error: <iframe src="main.html" height="100px&qu ...

Is it possible to vertically align variable length text within a container of fixed height?

One common method for centering text inside a div is to use the following CSS trick: .center-div { display:table-cell; vertical-align: middle; } The issue arises when the text within the div varies in length and I require the container to have fi ...

Ways to ensure a video completely fills the frame

I am experiencing an issue where the video in the main div of my home page does not fully fill the div right away - I have to refresh the page for this to happen. As a result, there is black space on the left and right sides. Interestingly enough, this pr ...