Adding a <div> within a <form> on button click using HTML and jQuery tactics

Whenever the '+' button [the addMore() function] is clicked, I want to insert the following div structure. Although I am able to add the div structure, the alignment of the input text is not equal as compared to the hard coded version.

Here is the HTML file:

<form class="align-center">
            <p class="6u 12u$(medium)">
                    Start Date: <input type="date" name="startdate" id="startdate" /> 
                    End Date: <input type="date" name="enddate" id="enddate" />
            </p>
            <p>
               <div class="6u$ 12u$(xsmall)"><input type="text" name="numParticipants" id="numParticipants" value="" placeholder="Number of Participants"/></div>
            </p>
           
           <!-- Remaining HTML code -->
           

And here's the relevant JavaScript function in main.js:

    var counter = 0;
function addMore() {
        counter++;
        // Javascript code to add new div elements goes here
}

This is how the page looks when loaded: when page loaded

You can see the actual and expected results here: actual and expected outcome

Answer №1

You may notice a discrepancy in the class names of the div elements that you are adding using jQuery.

To achieve the desired outcome, ensure that they match the static HTML content exactly.

The variations I have observed are as follows:

Static HTML shows -

<div class="3u 12u$(medium)">
, while your jQuery-generated HTML displays -
<div class="5u 12u$(medium)">

Static HTML contains -

<div class="3u 12u$(medium)">
, whereas your jQuery-created HTML contains -
<div class="5u 12u$(medium)">

Static HTML includes -

<div class="2u$ 12u$(medium)">
, but your jQuery HTML has -
<div class="3u$ 12u$(medium)">

The differences in class names are causing discrepancies in how the content is rendered on the user interface.

Answer №2

It looks like there is a missing div in your JavaScript inner HTML string. Here is how it should be corrected:

objNewDiv.innerHTML = '<div class="7u 10u$(medium)"> <div class="select-box"> <select> <option value="">-Select Option-</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select> </div> </div> <div>OR</div> <div class="8u 11u$(medium)"> <div class="13u$"> <input type="text" value="" placeholder="Customize your option"/> </div> </div> <div class="4u$ 9u$(medium)"> <div class="11u$"> <input type="text" value="" placeholder="Details" /> </div> </div> ';

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

Update the page using Ajax without relying on jQuery

I want to implement automatic page updates without the need for a refresh using Ajax. However, most resources I've come across suggest using jQuery and PHP, which are areas of coding I am not very familiar with. My goal is to integrate Ajax into my HT ...

Managing headers for localhost with Access-Control-Allow-Origin

I've run into a challenge with my React app. I'm making endpoint calls to different servers and have withCredentials set to true to include a token/cookie in the requests. The issue arises when trying to make this work seamlessly on localhost. S ...

Error: The value of "$tweetId" cannot be parsed as it is set to "undefined". Please ensure that string values are properly enclosed

I am utilizing sanity, and if you require more details, I will furnish it promptly. When I try to access http://localhost:3000/api/getComments, I encounter the following error message: ClientError: Unable to process value of "$tweetId=undefined". Kindly ...

How to set a default value for ng-model in AngularJS

Is there a way to set a default value on an angularjs model? I'm hoping for a solution similar to the following code: div.form-multiple(ng-model='story.r || []', form-multiple="form-multiple") Currently, the story.r is undefined and I woul ...

Disappearances of sliding jQuery divs

I am currently working on a website using jQuery, and I have implemented a slide in and out div to display share buttons. The issue I am facing is that the code works perfectly on the first page, but on every other page, the div slides out momentarily and ...

The Issue of Missing HTML Registration Form Data in Django

My configurations in demo\urls.py, user\urls.py, views.py, and HTML form seem to be set up correctly. However, upon pressing the submit button on the HTML form, I encounter a "File Not Found" error. Even after seeking assistance from ChatGPT, fol ...

What steps can I take to give priority to a particular ajax call?

Every 10 seconds, two ajax based methods are executed. However, when I submit the form for processing, it waits for the previous ajax calls to complete before processing. I want to prioritize the form submission. Below is the script that I am using: func ...

Is it possible to use AJAX to change the class name of a Font Awesome icon?

I am considering updating the icon after deleting a row. Should I initially include the font awesome icon once in the blade, then remove the class name and add it back with ajax? blade: <div id="status-{{ $country->id }}"> <div id ...

Preserve marked checkboxes upon page refresh

So I created a search engine with a filter using checkboxes in a form. The filter function is working fine when I submit the form (I'm using Flask for this). However, once the results are displayed, the checkboxes that were used as filters become unch ...

CSS-Enabled Panels held in place with Draggable Feature

Currently immersed in my first ASP.net/HTML/CSS Application, I am exploring the realm of draggable panels using the Ajax-Control-Toolkit. So far, it's been a successful endeavor as I have managed to implement a single panel into my application! As de ...

Creating a nested datatable from a JSON array object response is a valuable skill to have in

My API response is in JSON format with nested arrays. I am trying to parse it into a nested datatable, but I can't seem to get it working. Can someone please point out where I went wrong? The JSON data contains passenger information and each passenger ...

Displaying each character of text individually with jQuery

I am trying to display the text within a ul tag one by one when hovering over some text. However, I am encountering an error. How can I resolve this issue? You can view the code for mouseover functionality by hovering over the "hover here hover again" lin ...

Navigational bar customization: Troubleshooting search button functionality

I've been encountering some challenges with the search form in bootstrap 4.1's navbar. I've been referring to this documentation link for assistance. The search form I have set up appears quite typical for bootstrap: https://i.sstatic.net/I ...

Creating a Form with Dynamic HTML when Button is Clicked

I have been working on enhancing the functionality of my website app located at , but unfortunately, I have not been successful so far. My goal is to introduce a vendor information form with just one click of a button and then enable users to add products ...

Creating an embedded link to a website that adjusts to different screen sizes while also dynamically changing

I've been developing a personalized site builder that includes an iframe for previewing responsive websites. In order to make the site 'zoomed out' and not appear in mobile size, I'm utilizing CSS scale and transform origin as shown bel ...

The D3.extent() function is raising a TypeError because it is unable to iterate over the

I have been struggling with this issue for the past few hours, and I could really use some help. I'm not very familiar with using D3 in conjunction with React. Essentially, I have an anomaly detection algorithm running on the backend Flask App that r ...

Creating a React table with customizable columns and rows using JSON data sources

My query is this: What is the most effective way to dynamically display header names along with their respective rows? Currently, I am employing a basic react table for this purpose. As indicated in the table (2017-8, 2017-9, ....), I have manually entere ...

Eliminate error class in jQuery Validate once validation is successful

I'm having an issue with the jQuery Validate plugin. Even after a field is successfully validated, the "error-message box" continues to be displayed. How can I remove this box? Here's my code: CSS: .register-box .field .has-error{ border ...

Is it possible for an object to be null even when its type is object

There's an issue that I can't seem to replicate. Personally, I'm not experiencing any errors but bugsnag reports that some users are encountering them. Take a look at snippet line 6 for more information. let lang = this.$store.state.lang ...

Organize express controller by breaking it up into multiple separate files

Recently, I've set up an express js controller file as follows The File Path: /controllers/usersController.js // Register User module.exports.register = function(req, res) { ... } // Login User module.exports.login = function(req, res) { ... } // ...