Dynamically add extra input fields to html form

Posted on 2013-10-24
Last Modified: 2013-10-24
How can I dynamically add extra input fields to the following html form?

 <form action="submit.php" method="post">
        <input type="text" name="question" placeholder="Question" required /><br>
        <select name="mediatype">
            <option value="">Select Media Type</option>
            <option value="1">None</option>
            <option value="2">Image</option>
            <option value="3">Video Link</option>
            <option value="4">Video Embed</option>
        <input type="text" name="media" placeholder="Link">
        <input type="hidden" name="surveyid" value="<?php echo $surveyid;?>"/>
        <input type="hidden" name="submittype" value="3"/><br>
        <input type="hidden" name="questiontype" value="1"/><br>
        <input type="submit" value="Save Question"/>
Question by:N R
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 82

Accepted Solution

leakim971 earned 500 total points
ID: 39599037
Test page :

$(document).ready(function() {
   //$('<input type="text" name="textbox1" />').appendTo("form");
    $('<input type="text" name="textbox1" value="new" />').insertBefore("input:submit");

Open in new window

LVL 11

Author Comment

by:N R
ID: 39599063
That doesn't actually work the way expected.

I may have not explained correctly.

I'm looking for a completely separate button I would click that would add an additional input field to the form.
LVL 82

Expert Comment

ID: 39599069
Did you understood the code?
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

LVL 11

Author Comment

by:N R
ID: 39599071
What you posted on jfiddle didn't seem to do anything?
LVL 82

Expert Comment

ID: 39599074
It add a new field...
LVL 11

Author Comment

by:N R
ID: 39599076
Ah it seems what you posted was for it to insert an extra field when the page is loaded.  I need it to be done when  I click a button, but I'm not sure how to modify to do so?
LVL 82

Expert Comment

ID: 39599082
I dont see any button in your HTML let me think you want this clicking on a button.
Did you understood the code provided or do you just need someone to code for you?
LVL 11

Author Comment

by:N R
ID: 39599093
Yes I figured it out with some tweaking.

Now I'm going to open a new question.  How can I make the name of the new field go up in increments on 1 as I continue to add them?

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
boostsrap 1 34
Change javascript filter code from OR logic to AND 5 41
JQuery Data Table Default Sort 2 31
PDF Turn Look 7 34
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question