What causes the div to suddenly drop down after InnerHtml is modified?

I have a collection of thumbnail images and I want to update the content inside them upon clicking. However, whenever I add new content, they appear on a separate line instead of staying inline.

Here is the link to the jsfiddle example:

http://jsfiddle.net/jtDzs/

For instance, if I try to add "something" to the specific div with id "boogie":

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
      <title>TITLE</title>
      <style>
         .thumbContainer {
            width: 200px;
         }
         .thumb {
            width: 95px;
            height: 95px;
            background-color: blue;
            display: inline-block;
         }
      </style>
   </head>
      <div>
         <div class="thumbContainer">
            <div id="boogie" class="thumb"></div>
            <div class="thumb"></div>
         </div>
         <div class="thumbContainer">
            <div class="thumb"></div>
            <div class="thumb"></div>
         </div>
         <div class="thumbContainer">
            <div class="thumb"></div>
            <div class="thumb"></div>
         </div>
         <div class="thumbContainer">
            <div class="thumb"></div>
            <div class="thumb"></div>
         </div>
         <div class="thumbContainer">
            <div class="thumb"></div>
            <div class="thumb"></div>
         </div>
      </div>
      <script>
         (function() {
            $(".thumb").bind("click", function() {
               $("#boogie").html("something");
            });
         })();
      </script>
   </body>
</html>

Answer №1

It appears that the issue is being caused by using display: inline-block for some reason...

I have found two simple solutions to resolve this problem:

Solution 1: Overflow Hidden

To fix this, you can add the following line to your CSS class:

overflow: hidden;

You can view a working demonstration of this solution on JsFiddle


Solution 2: Float Left

Instead of using display: inline-block, you can try floating elements to the left and adding the desired margin.

// display: inline-block
float: left
margin: 2.5px;

You can also see a demo of this alternative solution on JsFiddle

P.S I assumed that you want this behavior for all divs, so I took the liberty of changing the html command to:

$(this).html("something");

Feel free to modify this as needed

Answer №4

         (function() {
            $(".thumb").on("click", function() {
             $(this).("something");
           });
          })();

CSS explanation:

     .thumb {
     color: white;
        width: 95px;
        height: 95px;
        background-color: blue;
        display: inline-block;
         float: left;
         margin: 2px;
     }

Check out the demo here!

Hope this information is useful.

Answer №5

 (function() {
        $(".thumb").html("&nbsp;");
        $(".thumb").bind("click", function() {
           $("#boogie").html("something");
        });
     })();


.thumbContainer {
        width: 200px;
        height:100px;
     }

Link to the JSFiddle example

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 is caused by an unhandled rejection in Node-ACL

In my express application, I am utilizing the ACL module. When the server starts, I define various roles and their corresponding permissions using the acl.allow() function. However, an error is being logged indicating an undefined rejection type. The erro ...

Styling HTML Select Box Options

My goal is to display the user's name and email within an option of a select box: <option>Adda <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caabaeaeab8aafb2aba7baa6">[email protected]</a></option& ...

Utilizing ng-repeat, ng-grid, and ng-click in Angular: A comprehensive guide

I'm encountering an issue with populating a table using ng-repeat after a cellTemplate ng-click. cellTemplate: '<div ng-click="foo()" ng-bind="row.getProperty(col.field)"></div>' Within this foo method, I am tryi ...

Validation in ASP.Net to ensure the correct number of days between the arrival and departure dates

One of my project requirements involves checking the validation of the number of days entered between two date selectors (From & To Dates). The constraint is that the difference should not exceed 100 days. I'm considering using ASP.NET validators for ...

What is the best way to include a variable or literal as a value in styled components?

When it comes to managing various use cases, I always rely on props. However, I am currently facing a challenge in changing the border color of a styled input during its focus state. Is there a way to utilize props for this specific scenario? Despite my f ...

Here are the steps to follow in order to click on a cloned label, change the checkbox's checked property to false, then click on

I am currently working on improving a search filter feature. When users click on the filters (checkboxes and radio buttons), I want to display a clone of the corresponding label in a header section. If a user clicks on the checkbox or radio button again, t ...

Synchronize information between two drop-down lists

I am currently utilizing the boostrap library to create a pair of dropdown lists. My goal is to have the items in the second dropdown list dynamically update based on the selection made in the first dropdown. Within my code, there exists a dictionary name ...

AJAX does not support functioning links

I've been experimenting with AJAX on my website, and encountered an issue. When I dynamically add content to the page using container.insertAdjacentHTML('beforeend', thing); everything looks fine. However, when I try to click on the links wi ...

Arrange a string with specified delimiters based on the json field

Here is an example located at This JavaScript code reads and displays the data: let dataString = "Worl, Seymour|Jones, Jim|Smith, Paul|Jolly, Roger|"; let splitString = dataString.split("|"); for (let i = 0; i < splitString.length;) { $("#I" + i) ...

Does this function only function on a singular div?

I need some help! I'm having trouble getting a function to trigger on the divs below the initial one. Any advice? ...

Is there a way to use jQuery to centrally align the accordion item that I have chosen?

My attempts to use `this.scrollIntoView({behavior: "smooth"}) were yielding inconsistent results in terms of where the selected item would land on the page. I believe I might need to utilize scrollTop(), but as someone who is new to jQuery, I am ...

Switching from a basic material to a complex multi-material in three.js during runtime

r65 Hello, I am trying to switch from using a simple material to multi-material at runtime, but I am facing some difficulties. Could it be that I am overlooking something obvious? Below is the test code I am working with (you can also view it on jsfiddl ...

Are node.js and AngularJS connected in any way?

Node.js relies on npm for package management, while AngularJS CLI also uses npm for its modules. Is there a connection between these two? I have installed Node.js and tested it with a simple 'hello.js' file containing just one line of code: con ...

Transform any falsy values within a JavaScript object into an empty string

Looking for a solution to transform all undefined, NaN, etc. values in a Javascript object into an empty string for better definition. For example: javascriptObject = convertUndefined(javascriptObject) Seeking something that can achieve this functionalit ...

Error: Controller not found when angular.module is called from different files

As I am restructuring my code to make it more modular, I decided to move the controller into a separate file that belongs to the same module. However, I am facing an issue where the controller does not load, even though I have verified that the load order ...

How to delay the return of an ajax function in jQuery only upon success?

I have some code set up and I am looking to create a straightforward API for ajax requests. My goal is to utilize deferred in order to achieve success. var factory = function(dataParams) { return $.ajax({ type : "POST", dataType ...

JavaScript: incorporating PHP into JavaScript

What is the proper way to incorporate PHP inside Javascript? var timeDiff = 60-20; if ( timeDiff <= 60 ) { var stamp = timeDiff <?php echo $this->__('second ago') . '.'; ?>; } A console error has been detected: Unca ...

Guide on utilizing protractor to confirm equality of two spans in varying positions?

<span ng-bind="locations.selectedCount" class="ng-binding">1005</span> <span ng-bind="locations.selectedCount" class="ng-binding">1005</span> What method can I use in Protractor to verify that the values of these two spans are ide ...

The ajax function in jQuery seems to be failing to retrieve data from a webmethod

I have created a jQuery function that is triggered by a DOM event: function CalculateAdhesiveWeight(volatiles1, volatiles2, testedWeight) { var volatiles1 = $('#form-textVolatiles1').val(); var volatiles2 = $('#form ...

Retrieve numerical data from the element and enclose it within a span tag

My goal was to indent a number, but the HTML generated from my CMS is making it difficult to target the number and apply a specific style. <div class="content"> <b>01. Header</b> Lorem ipsum dolor sit amet, consectetur adipisicing elit, ...