Add multiple controls to a div element

Working efficiently, I've set up a system to collect data within a div by inserting hidden input boxes before submitting it to the server. Below is the JavaScript code I'm using:

function appendToDiv()
{
 var mydiv=document.getElementById("somediv");
 var mydata=document.getElementsByName("description")[0].value;
 var myurl=document.getElementsByName("url")[0].value;
 var data=mydata+myurl;
 mydiv.innerHTML="<input type='hidden' name='sUrl[]' value='"+data+"'/>"
}

I have an onchange event in place that continuously calls the above function until I have gathered all necessary data to send to the server. However, only one input gets appended to the div. What could be causing this issue?

Answer №1

Every time you use innerHTML="...", you are replacing the existing content. It is better to create your inputs as DOM objects (document.createElement) and then use appendChild to add them.

I did a quick search and found a page that explains this concept:

You can implement it like this:

var el = document.createElement('input');
el.setAttribute('type', 'hidden');
el.setAttribute('name', sUrl[]); // I'm not sure what sUrl[] is supposed to be?
el.setAttribute('value', data);
mydiv.appendChild(el);

By the way, most frameworks (like jQuery, Ext JS, etc.) make tasks like this much simpler if you use them regularly.

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

Uploading to npm unsuccessful as the specified file in the `main` attribute was not successfully published

I am currently facing an issue while trying to publish a package on npm. The .js file I specified in the package.json is not being included with the package. My project is built using typescript, and I transpile it using the following npm script... "prepu ...

Executing a Javascript GET request leading to a 301 redirect

Struggling to identify the issue at hand, I have turned to this platform in hopes of finding a solution. The setup involves an angularJS app with a GoLang/Gorilla mux server backend. The web app runs on http://localhost:8888/, while the server operates at ...

Swapping XML tag names in Node.js

I'm trying to use nodejs to rename all tags in an XML file. My initial thought was to utilize regex and the fs module to read the file, but I quickly felt overwhelmed. <RESULTS> <ROW> <COLUMN NAME="DATA_CRIACAO"><![CD ...

Using Javascript to swap out elements sharing the same classname with distinct contents

Just a quick question: Can you replace the values of elements with the SAME class with different values? For example, let's say I have this array returned by an AJAX response: [ "$63.00 / Night", "$68.00 / Night", "$58.00 / Night", "$50.00 / ...

The seamless integration of React.js with HTML

My friend and I are beginners in the world of programming, with a solid grasp of html, CSS, and JavaScript. We're currently collaborating on a small project where we aim to create a chat system. While researching resources for our project, we came acr ...

Marked checkboxes and Node.js

I'm having trouble grasping the concept of using HTML checkboxes with Node.js and Express. I have a basic form in EJS and before diving deeper into the backend logic, I want to ensure that the correct values are being retrieved. Despite my efforts to ...

The ordering of jQuery's each() and load() functions

I am working on a script that iterates over a series of JSON objects representing images. As the loop loads each image, it is appended to an element within my website or application. Although the images are correctly ordered in the JSON, they sometimes ap ...

Link to Reply Comment Feature in Wordpress

It seems like I've tried numerous solutions to fix my issue but haven't had any luck, so now I'm seeking help here. Despite trying methods that have worked for others, they do not seem to work for me. My problem lies with nested comments on ...

Unable to upload a file to an email using the mandrillapp JSON API

Having trouble sending an email with an attached document via the Mandrillapp JSON API using the method send-template in JavaScript. The email sends successfully, including images attached to the images array. However, documents sent in the attachements ar ...

Having trouble inputting a value into a textbox after enabling it via Javascript

I'm having trouble setting a value in a textbox after forcibly enabling it through JavaScript executor in my Selenium automation script using Ruby bindings. input_fieldcar1 = browser.find_element(:xpath, "/html/body/div[5]/div/div[3]/div[2]/div[2]/di ...

JavaScript fails to execute in Prestashop

Can anyone help me troubleshoot my countdown shipping time code? Here is the script I am using: http://jsfiddle.net/37ox54bk/7/ I am utilizing the HTML box module found here: This is how the code looks: <div id="countdownTimer">0</div> ...

A common inquiry: how can one pinpoint a location within an irregular figure?

I have been studying Html5 Canvas for a few weeks now, and the challenge mentioned above has puzzled me for quite some time. An irregular shape could be a circle, rectangle, ellipse, polygon, or a path constructed by various lines and bezier curves... I ...

React: Using useState and useEffect to dynamically gather a real-time collection of 10 items

When I type a keystroke, I want to retrieve 10 usernames. Currently, I only get a username back if it exactly matches a username in the jsonplaceholder list. For example, if I type "Karia", nothing shows up until I type "Karianne". What I'm looking f ...

Changing the default date display in Vue.js

Is there a way to automatically set today's date as the default date on the datepicker? Here is the code snippet: <template lang="html"> <input type="date" v-model="date"> </template> <script lang="js"> export default { nam ...

Getting parameter names (or retrieving arguments as an object) within a method decorator in TypeScript: What you need to know

I am currently working on creating a method decorator that logs the method name, its arguments, and result after execution. However, I want to implement a filter that allows me to choose which parameters are logged. Since the number and names of parameter ...

Using Angular filters, it is possible to eliminate DOM elements without relying on jQuery

As I dive into learning Angular, I am grasping the basics quite well. However, my limited knowledge of JavaScript seems to be hindering my progress. Currently, I am working on a feed that pulls data from a JSON file. Within this data, there is a string co ...

Tips for preventing scrolling on iOS in Chrome or Safari using CSS, JavaScript, or jQuery

I've successfully implemented an input element with a click event listener that triggers a function to make another element visible using the CSS rule "display:block;". The element in question has the following styling rules: .elementExample { d ...

Transferring information from a single document to numerous documents

I have been developing an internal webpage to streamline email automation tasks. Currently, I have created a basic form that contains general information which will be consistent across all subsequent forms. Additionally, there are multiple customized form ...

How can I achieve the function of // @codekit-prepend "" with gulp?

I have recently started using gulp and I am familiar with codekit where I used to write // @codekit-prepend "foo.js" // @codekit-prepend "bar.js" to concatenate js files. Can someone guide me on how to achieve the same result with gulp? Below is the ...

The process of dynamically adding buttons in Summernote with the inclusion of a parameter

Currently, I am working on developing dynamic custom buttons for my summernote wysiwygs. However, I have encountered an issue. My goal is to pass the data that I am iterating through to the button's render function. Unfortunately, since the context is ...