Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:35:25+00:00 2026-06-04T11:35:25+00:00

I am having trouble with generating text fields dynamically. I have a select box

  • 0

I am having trouble with generating text fields dynamically. I have a select box in the form and what I want to do is to create text fields on the basis of selection of number in drop down list. So if the user selects one I want to create one text field below it. I am using javascript and onChange event to accomplish it. But the code is not working in any of the browsers.

Here is The javascript function I am using in the head section of the document:

function addField() 
 {
 alert("Hello");
 var num=document.forms["myForm"]["timePerDay"].value;
   for(var i=0;i<num;i++)
    {
    var element = document.createElement("input");


    element.setAttribute("type", input);
    element.setAttribute("name", time[] );

    var foo = document.getElementById("fooBar");

    //Append the element in page (in span).
    foo.appendChild(element);
    }
    }

And Select field is:

 <select name="timePerDay" onChange="javascript:addField();"> 
<option selected="selected">Select One</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

<span id="fooBar">
</span>

It seems like onChange is not firing at all in any browser as I tried to put alert message in the JS function and even it is not working. So can you please tell me what is the problem.
You can see the page at : http://motushealth.com/form1.html

PS:
I am using another JS function on the form to validate it and it works on Onsubmit . It is working perfectly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:35:27+00:00Added an answer on June 4, 2026 at 11:35 am

    The problem is a syntax error here: element.setAttribute("name", time[] ); time[] is valid in php, but not in js.

    EDIT:

    The alternate approach is to not use inline js (...onClick='javascript:addField();'...). Instead do the following on window.onload or better, on the onDomReady event.

    JS:

    window.onload = function() {  /* or window.addEventListener */
        document.getElementById('timePerDay').onchange = addField;  /* or addEventListener */
    };
    

    And markup can be:

    <select id="timePerDay" name="timePerDay"> 
        <option selected="selected">Select One</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble generating a web form using fields specified in an
I'm having trouble on generating a report in MySQL. I have this table: CREATE
I'm having trouble generating a nested model form. Here are my models: class Workout
I'm having real trouble generating a text file per 'operation' to store operation specific
I am having trouble with Subsonic 3.0 generating an object for a table which
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
Having trouble with a mysql grant statement. I want a user who has: readonly
My eclipse program is having trouble generating the R file for any project that
I am having trouble getting my filters/validators to work correctly on my form, so
Problem: I am having trouble generating an xpath expression that selects a node in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.