The jQuery append function does not take into account the styling applied to the div

Whenever I click the "add item" button, I want to append a new line (a text input) to the main content. This functionality is working correctly.

However, the issue arises when adding the text input using jQuery. The CSS styling, including width and height properties, is not being applied to the newly added text input. As a result, the text input appears as a default unstyled version rather than the custom styled version I intended.

Below is the relevant HTML code:


<fieldset class="lodging-info-fieldset" id="all_field">
    <legend>Gage sur équipement projet</legend>
    <div class="panel-heading score_pannel">
        <ul class="panel-heading-fieldset">
            <li>Désignation</li>
            <li>Valeur</li>
        </ul>
    </div>
    <div class="form-group" id="extended">
        <div class="col50 left">
            <input type="text" class="form-control" id="designation" placeholder="">
        </div>
        <div class="col50 left">
            <input type="text" class="form-control" id="valeur" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <div class="add_plus">
            <a id="add_pluss" class='add_plus'>add item </a>
        </div>
    </div>
</fieldset>

And here is my jQuery append function:


addLigne : function(event) {
    $("#all_field").append(
        '<div class="form-group">' +
        '<div class="col50 left">' +
        '<input type="text" class="form-control" id="designation">' +
        '</div></div>');
},

Can anyone suggest a solution to this problem?

Thank you for your assistance!

Answer №1

It appears that you may be duplicating and overcomplicating the same ID element in your code. This practice is not recommended as it can cause issues with accessing the element correctly. Consider using unique identifiers, such as numbers, to avoid confusion.

addLigne : function(event) {
    $("#all_field")
        .append(
              '<div class="form-group">'
            + '<div class="col50 left">'
            + '<input type="text" class="form-control" id="designation_'+ ($('input[id^="designation_"]).length + 1) +'">'
            + '</div></div>');
},

If you share your CSS code, we may be able to provide further assistance with your issue.

Best of luck with your project!

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

Having difficulty formatting text alignment using CSS

Is there a way to maintain the alignment of text in the output of the 'About' section even when the content changes dynamically? I want the new line to appear just below 'Lorem', but currently, it shifts below the colon(:). Since the le ...

Exploring deep within JSON data using jQuery or Javascript

I have a substantial JSON file with nested data that I utilize to populate a treeview. My goal is to search through this treeview's data using text input and retrieve all matching nodes along with their parent nodes to maintain the structure of the tr ...

By clicking anywhere, AngularJS will remove the specified class

Imagine having a search box that is displayed after the user clicks on a button. Great so far, right? But what if you want to add another feature - making the search box disappear when the user clicks anywhere else. How can this be achieved with AngularJS? ...

Retrieve the ultimate information from the Angular service

Within my angular 6 project, I am dealing with a product_id array, product_id: any = ["123", "456"]; ngOnInit : ngOnInit() { this.product_id.forEach(element => { this.httpClient.get('https://api.myjson.com/bins/hiolc').subscribe ...

KineticJs: Enhancing Rotation with Multitouch Capability

Currently, I have a click event implemented in my code that rotates my puzzle piece by 90 degrees when clicked. However, I would like to change it from a mouse click to a touch event. How can I achieve this? Thank you. piecesArray[i][j].shape.on("mous ...

What are some tips for using CSS display grid to effectively showcase my HTML content?

Visit my About Me page with custom menu styling Any tips on aligning the body with the grid function to achieve this particular design? ...

What is the best way to manage asynchronous functions when using Axios in Vue.js?

When I refactor code, I like to split it into three separate files for better organization. In Users.vue, I have a method called getUsers that looks like this: getUsers() { this.isLoading = true this.$store .dispatch('auth/getVal ...

How can I align two div buttons at the top and bottom to appear on the left and right sides?

How can I change the position of two buttons from top and bottom to left and right as shown in the second image? I am utilizing Floating Vue, so the buttons will be contained within a div. Is it feasible to adjust their position accordingly? Check out th ...

Having difficulty updating the parent for all PortfolioItem/Feature that were copied for a specific PortfolioItem/MMF

I'm facing a challenge in setting the parent for multiple features that I've copied for a specific MMF. However, only the parent of the last feature is being set. Below is the code snippet responsible for setting the parent: Record represents th ...

Why is it that my JQuery sliders appear perfectly fine when viewed locally, but fail to show up when accessed on

I'm facing an issue with two JQuery sliders on my page. The local version works fine, but when I upload it to my web host, only one slider functions correctly. I need both of them to work as intended. Any insights on how to resolve this problem? See ...

Using JavaScript, redirect to a specified URL once a valid password has been entered on an HTML form

Need help with JavaScript for password validation and URL redirection. I'm new to JS and used some resources like Password correct? then redirect & Adding an onclick function to go to url in javascript?. Here's my current code: ...

Tips for sending a command to the server using the ssh2-promise package in Node.js

In my current code, I am utilizing an SSH library to establish a connection to a server, create a shell session, and send a password to authenticate. However, I am encountering an issue where the password is not being sent as intended. Upon some debugging ...

Displaying a Progress Bar while Inserting Data into a Database Using Asp.net

When I click on the insert/update button, I want to display a progress bar. However, due to some validations, updations, and insertion functions in the buttons, I am unable to calculate the time taken for a particular button click process. The following ...

Using the MoveToElement and click functions in Protractor with Node.js for automated browser

Trying to click on a checkbox in our application. I have successfully implemented it in Selenium Java using the code below, but struggling to do the same in Protractor Node.js. Any assistance would be appreciated. Selenium- Java : Actions actions ...

How to send props from a Vue.js component tag in an HTML file

I'm facing an issue with passing props from the HTML to the JavaScript code and then down to a Vue component. Here's a snippet of my index.html file: <div id="js-group-discounts"> <div class="form-group required"> <datepick ...

How can the outer function be connected to the resolve method of $routeProvider?

Here is a functional code snippet: $routeProvider.when('/clients', { templateUrl:'/views/clients.html', controller:'clientsController', resolve: { rights: function ( ...

CSS Background color only applies to the lower section of the page

I'm attempting to create a social media button using an anchor tag and a fontawesome icon. My goal is to have the entire background colored black with CSS, but my previous attempts only resulted in the bottom half turning black. https://i.sstatic.net ...

Modify the text in a paragraph by clicking on a link using JQuery and HTML

I'm attempting to implement a straightforward action, but I'm facing some challenges. My navigation bar consists of a few links followed by a text section. What I aim for is that when I click on a link, the paragraph of text should change to refl ...

Utilize visuals to select premade designs

I am looking to enhance the user experience in selecting templates on Wordpress by integrating images for a visual preview. Instead of a traditional text-based dropdown menu, users would be able to see what the template looks like before choosing it. EDIT ...

JavaScript inheritance through prototypes and the properties of objects

I am currently exploring the concept of prototyped inheritance in JavaScript for a function. This process is well documented in Wikipedia's javascript article. It functions smoothly when dealing with simple JavaScript types: function Person() { t ...