The padding of dynamically generated bootstrap buttons diminishes

I am facing an issue with a bootstrap button bar that is using the well class. The problem occurs when I create it twice in my code: once using HTML and another using dynamic javascript.

The bootstrap functions properly when created with HTML.

However, the buttons lose their padding when dynamically generated with javascript.

My expectation is to have consistent horizontal spacing in both instances.

Why does the dynamically created version not maintain the original formatting present in the HTML-created one?

If anyone has a solution to this, please assist me.

Original code:

<div class="well">
    <button type="button" class="btn btn-primary btn-xs">Button 1</button>
    <button type="button" class="btn btn-primary btn-xs">Button 2</button>
    <button type="button" class="btn btn-primary btn-xs">Button 3</button>
    <small class="pull-right">Right Text</small>
</div>


<div id="myMenu">
</div>

<script type="text/javascript">

    $(document).ready(function () {

        var upperWell = $("<div class='well clearfix'>");

        $('#myMenu').append(upperWell);

        var createButton = $("<button type='button' class='btn btn-primary btn-xs'>Button1</button>");

        var updateButton = $("<button type='button' class='btn btn-primary btn-xs'>Button 2</button>");

        var exportButton = $("<button type='button' class='btn btn-primary btn-xs'>Button 3</button>");

        $(upperWell).append(createButton);
        $(upperWell).append(updateButton);
        $(upperWell).append(exportButton);

    });

</script>

The JsFiddle for reference:

JsFiddle

Your assistance is greatly appreciated.

Answer №1

There is whitespace between the static buttons, but not between the appended ones. To add space between each button...

$(upperWell).append(createButton).append(" ");
$(upperWell).append(updateButton).append(" ");
$(upperWell).append(exportButton);

Check out this Fiddle for reference.

Alternatively, you can add space after each button once they are appended...

$(upperWell).append(createButton);
$(upperWell).append(updateButton);
$(upperWell).append(exportButton);

$("#myMenu button").after(" ");

View this Fiddle for further details.

Answer №2

Applying the display: inline-block; property to an element can result in a small gap between two elements.

For more information, you can visit this link: http://css-tricks.com/fighting-the-space-between-inline-block-elements/

If there is no space between elements (such as in buttons created by JavaScript), the gap will not be visible.

In cases where buttons are dynamically generated by JavaScript, they are appended to the HTML code without any spacing.

Original code snippet:

<div class="well">
    <button type="button" class="btn btn-primary btn-xs">Button 1</button>
    <button type="button" class="btn btn-primary btn-xs">Button 2</button>
    <button type="button" class="btn btn-primary btn-xs">Button 3</button>
    <small class="pull-right">Right Text</small>
</div>

Dynamically generated code snippet:

<div class="well clearfix"><button type="button" class="btn btn-primary btn-xs">Button 1</button><button type="button" class="btn btn-primary btn-xs">Button 2</button><button type="button" class="btn btn-primary btn-xs">Button 3</button></div>

To address this issue, you can use the following CSS code:

#myMenu button {
   margin-right: 4px;
}

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

Exploring the images in a continuous loop

Looking to create a unique image looping effect using HTML, CSS, and Jquery/Javascript. The concept is to display several images on one page that do not move, but loop through them one at a time while displaying text above the current image. For example, ...

Should the use of readFileSync() during the initialization of a Node.js web application be avoided?

I have a Node app that serves web pages with static HTML-snippet files included conditionally. I am considering implementing a cache map for these snippets by adding the following code to my Express's app.js file: var cache = Object.create(null); cac ...

How can I determine which component the input is coming from when I have several components of the same type?

After selecting two dates and clicking submit in the daterange picker, a callback function is triggered. I have two separate daterange pickers for SIM dates and Phone dates. How can I differentiate in the callback function when the user submits dates from ...

Change the appearance of a div based on the presence of a certain class within a child div using jQuery styling techniques

I'm trying to set a default padding of 15px for div.content, but if it contains a child div.products-list, I want the padding to be removed. I've looked into solutions using jquery, but nothing seems to work. Here's how my layout is structur ...

Discovering image file extensions in JavaScript using regular expressions

Can anyone provide me with a javascript regular expression to validate image file extensions? ...

Using jQuery to replace the content of a div with a delay?

I am attempting to utilize jQuery to create a fade effect on an element, replace its innerHTML content, and then fade it back in once the new content has been added. I have successfully managed to replace the element's content using the .html() method ...

Different width can be set for the Google Maps template specifically for mobile mode by following these steps

Is there a way to adjust the width of a Google Maps template specifically for mobile mode? While it looks perfect on desktop, the map overflows the screen size on mobile. See my code snippet below: @section ('map') <div id="map"></d ...

Column Locking with Merged Rows

I have implemented row spanning in jqgrid by following the instructions provided in this answer: Jqgrid - grouping row level data However, I am facing an issue where setting a column with row span to frozen = true causes the overlay to lose the row spanni ...

Updating parent component's scrollHeight of DOM element based on child component in Next.js

I recently encountered an issue with nested accordions. In my project, I have implemented accordions within other accordions, but ran into a problem when the inner accordion expanded and affected the size of the parent accordion. Here's what's ha ...

Customize a web template using HTML5, JavaScript, and jQuery, then download it to your local device

I am currently working on developing a website that allows clients to set up certain settings, which can then be written to a file within my project's filesystem rather than their own. This file will then have some parts overwritten and must be saved ...

The default selection in res.format is not being properly recognized

When I make a request with the accept header set as "application/json, text/javascript, */*; q=0.01", the response is always in HTML format instead of the default format specified. It seems like something is missing here. Given that the header does not spe ...

looking to display the latest status value in a separate component

I am interested in monitoring when a mutation is called and updates a status. I have created a component that displays the count of database table rows when an API call is made. Below is the store I have implemented: const state = { opportunity: "" } ...

Anchor elements and other inline elements not triggering MouseLeave event

Take a look at this CodePen link, particularly in Chrome: https://codepen.io/pkroupoderov/pen/jdRowv Sometimes, the mouseLeave event doesn't trigger when a user quickly moves the mouse over multiple images. This results in some images still having t ...

Having some trouble implementing the functionality of hiding a data series in HighCharts using `{ data: [], visible: false }` notation

I am currently working on creating a graph that displays multiple series obtained from JSON data in a MySQL database. My goal is to have the graph show only one series initially. Thankfully, HighCharts offers an option to achieve this as demonstrated below ...

Adjust the size of an image within a container in real-time with jQuery

In my coding project, I have a div element called "content_container" that is designed to be draggable and resizable using jQuery UI. Within this container, I have integrated TinyMCE, a content text editor. My current challenge involves the scenario where ...

The horizontal scrollbar in Chrome is unexpectedly activated, despite elements being set to occupy the full screen width (100vw) using Tailwind and NextJS 13.4+

It took some time to identify the root cause of this issue within a larger project. Hopefully, this Question and Answer will be beneficial to someone else. Here is the scenario -- in a NextJS/Tailwind project, there is only one large vertical element on t ...

Using Node.js to render when a task has been completed

I am currently developing a Node.js Application using Express.js. One of the challenges I face is rendering data from another site on a page using Cheerio.js. While this in itself is not an issue, I struggle with determining how to render the data once the ...

Guide to sorting data by the status value within a JavaScript object?

I have a JavaScript object structured like this: { "3": { "id": 3, "first": "Lisa", "last": "Morgan", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd7d6d4c9dcdad5fbdcd6dad2d795d8d4d6">[email&# ...

Trigger callback function when user selects a date on the calendar using Material UI X Date Picker

I am currently utilizing the DateTimePicker component in material ui, version 5. I am looking for a way to intercept the callback triggered when a user clicks on a day in the calendar selector: https://i.stack.imgur.com/0Tlogm.png Although the DateTimePi ...

Confusing jQuery Selector Problem

Here is an example I have What this example should do: Check if there is a .box-wrapper in the document If there is, and if there is a click event on .tabs li a Locate .selected and remove the class Find the parent of the clicked link and add the class ...