Struggle with dynamically placing Checkboxes in the right spots

When trying to create dynamic CheckBox elements, I encounter issues with positioning them correctly. I aim to place them below the input field instead of below the Image. However, I am unsure of how to achieve this specific placement.

Demo

HTML:

 <div id="modalDialog">
    <form>
        <p>Description:</p>
        <input type="text" id="customTextBox" style="width: 100%; font-size: 120%;" />
        <hr class="fancy-line" />
        <p>Card due Date:</p>
        <input type="text" id="datepicker" style="width: 15%" />
        <input type="button" id="Getbtn" value="Get value" />
        <hr class="fancy-line" />
        <p>Things To Do:</p>
        <div id="progressbar">
            <div id="progress">
                <div id="pbaranim"></div>
            </div>
        </div>
        <p>Create CheckBox:</p>
        <input type="text" id="checkBoxName" />
        <input type="button" id="btnSaveCheckBox" value="_Ok" />
        <hr class="fancy-line" />
        <p>
            <img src="/Pages/Images/comment.png" width="40" height="40" style="display: inline-block" />Comments:</p>
    </form>
</div>

JQuery:

$(document).ready(function () {
    $('#btnSaveCheckBox').click(function () {
        addCheckbox($('#checkBoxName').val());
        $('#checkBoxName').val("");
    });

    $(function () {
        $("#progressbar").progressbar({
            value: 0,
            max: 100
        });

    });
});

function addCheckbox(name) {
    var container = $('#modalDialog');
    var inputs = container.find('input');
    var id = inputs.length + 1;

    $('<input />', {
        type: 'checkbox',
        id: 'cb' + id,
        value: name
    }).appendTo(container);
    $('<label />', {
        'for': 'cb' + id,
        text: name
    }).appendTo(container);
    $('<br/>').appendTo(container);
}

Answer №1

The reason for this issue is that your code is appending the checkboxes to the modalDialog element, which serves as the container for all visible items. Consequently, they are placed after everything else.

To resolve this, you can create a new element (e.g., named checkboxes), position it where you desire the checkboxes to be displayed, and then append the checkboxes to this element. Here is an example:

Demo Fiddle

HTML

<div id="modalDialog">
    <form>
        <p>Description:</p>
        <input type="text" id="customTextBox" style="width: 100%; font-size: 120%;" />
        <hr class="fancy-line" />
        <p>Card due Date:</p>
        <input type="text" id="datepicker" style="width: 15%" />
        <input type="button" id="Getbtn" value="Get value" />
        <hr class="fancy-line" />
        <p>Things To Do:</p>
        <div id="progressbar">
            <div id="progress">
                <div id="pbaranim"></div>
            </div>
        </div>
        <p>Create CheckBox:</p>
        <input type="text" id="checkBoxName" />
        <input type="button" id="btnSaveCheckBox" value="_Ok" />
        <div id='checkboxes'></div>
        <hr class="fancy-line" />
        <p>
            <img src="/Pages/Images/comment.png" width="40" height="40" style="display: inline-block" />Comments:</p>
    </form>
</div>

jQuery

$(document).ready(function () {
    $('#btnSaveCheckBox').click(function () {
        addCheckbox($('#checkBoxName').val());
        $('#checkBoxName').val("");
    });

    $(function () {
        $("#progressbar").progressbar({
            value: 0,
            max: 100
        });

    });
});

function addCheckbox(name) {
    var container = $('#checkboxes');
    var inputs = container.find('input');
    var id = inputs.length + 1;

    $('<input />', {
        type: 'checkbox',
        id: 'cb' + id,
        value: name
    }).appendTo(container);
    $('<label />', {
        'for': 'cb' + id,
        text: name
    }).appendTo(container);
    $('<br/>').appendTo(container);
}

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

The button is unable to contain all the text, causing it to spill out instead of wrapping to

Need help with creating an HTML button that opens a link. The button consists of a title and a short description, but when the description is too long, it overflows outside the button. Currently implementing bootstrap and jquery. Code: body { backgr ...

Chrome fails to select item in Protractor test, while Safari succeeds

While my test passes smoothly on Safari, I encountered an issue on Chrome where the following code snippet fails to work: it('should click the first source and get to the source preview page', function () { var grid_icon = element(by.css(&a ...

Concerns regarding the functionality of form generation using "Recurly.js" alongside GWT and AJAX

Currently, I am in the process of developing a user interface within a GWT application for managing Recurly accounts. This setup eliminates traditional browser navigation when moving between "pages" within the app, allowing the client DOM state to be retai ...

HTML div feedback container with directional pointer

I've been working on creating a comment box with an arrow using CSS, but I'm facing a particular challenge. My comment box has a white background, and in order to make it stand out, I need to add a border. However, I'm struggling with addin ...

Discover the longest substring in a string without any repeated characters

I'm looking to identify the longest substring within a given string. const str = "aassqwertybbvvqwertyuivv"; const longestSubstring = str.split("").reduce((prevValue, currValue, index, arr) => { let substr = "" ...

Interactive sidebar scrolling feature linked with the main content area

I am working with a layout that uses flexboxes for structure. Both the fixed sidebar and main container have scroll functionality. However, I have encountered an issue where scrolling in the sidebar causes the scroll in the main container to activate whe ...

What is the best way to align a container in the middle of the page without positioning the text in the center

Query 1: What is the best way to horizontally center the container div while keeping Typography aligned to the left? Expected outcome: https://i.stack.imgur.com/yKCZF.jpg Query 2: How can a component be positioned below the AppBar without relying on mar ...

How can you determine if an SVG element has been resized, such as through a resize event?

Within a <div>, I have an embedded <svg> element. Whenever the parent <div> is resized, the <svg> drawing also resizes perfectly. My query is: Is there a method to receive notification after the resizing of the <svg> elemen ...

Issue with Bootstrap 4 navigation tabs: dropdown items do not cycle through active tab panels

Whenever I utilize nav-tabs to toggle between tabpanels, it functions properly. However, there seems to be an issue when attempting to switch the active tabpanel using the links in the dropdown menu. Below is the HTML for my nav-tabs and dropdown menu, alo ...

Upon page load, a dazzling SVG file will flicker before being adorned with CSS styles

I'm experiencing an issue with an SVG arrow icon on my webpage that flashes quickly across the entire screen upon page load before settling into its proper size and placement. I've tried using CSS to initially hide the icon and then reveal it aft ...

Utilize PHP to conduct a Json comparison and perform an insertion

Since receiving assistance from @rx2347, everything is functioning correctly except for one issue. I have revised my question to provide a clearer explanation of the problem. I have a JSON array from which I am extracting specific data such as 'id&ap ...

How can you verify the inputted age and execute the correct conditional statement?

Currently, I am going back to basics in JavaScript and realizing that my foundation is not as strong as I had assumed. It seems like I need a fresh perspective because I can't seem to pinpoint where the code error lies. (A lot of the resources I foun ...

Problem encountered while generating a torus shape using webGL

I am currently developing a program that aims to create 3D parametric shapes using webgl. The current code I have works successfully for rendering a sphere, but when I try switching the equations for a torus, only the upper half of the torus is being displ ...

Arranging an array of objects based on dual criteria

Sorting an array of objects based on 2 conditions is my current challenge First, I need to sort by value If the names are the same, I want to display them next to each other in ascending order of their values For a visual example, check out this demo: ht ...

servlet failing to send a response to ajax call

I am facing an issue where the servlet is not sending back a response to my AJAX code. Can someone please help me with this? Below is the HTML code, the output should be printed here: This is the AJAX code written in JavaScript: <script language="jav ...

Dealing with server-side errors using ajax technology

Hello, what is the best way to handle server exceptions using ajax and jquery? I am currently working on a web application using Java and the struts framework. In my project, I have been managing exceptions using global exception handling: <Global-exc ...

Guide on sending a JavaScript variable to PHP using AJAX

I am currently utilizing the following JavaScript code to obtain data from a td element when a button is clicked within an onclick event. function rfk(element) { var element = element.parentElement.parentElement; var id = parseInt(element.childre ...

Securing socket.io connection in a nodejs application

I'm currently developing a chat application that utilizes sockets for bi-directional message sharing. Although the socket is functioning properly, I would like to implement user authentication before granting access to the socket connection. I'v ...

Can someone guide me on how to make an array filled with dates using Javascript?

I am working on developing a Javascript code that can identify all the days leading up to the current date. Here is what I have managed so far: var titleArray = [ "title1", "title2", ]; var pictureArray = today.toString(); var thumbArray = today.toString ...

execute task to optimize CSS encoding in the build process

Creating a static project with yeoman -webapp I've implemented a UTF checkmark in my .scss files: [type="checkbox"]:checked + label:after { content: '✔'; position: absolute; top: 3px; left: 0px; font-size: 22px; color:$color-pr ...