1

In this code I'm trying to generate dynamic text fields based on the input of select field which handled by addInput(divname) function using on change event but the while loop inside addInput function is not working and when i remove while loop its working. i have to generate selected no. of text fields... and also the text fields should change when i select different number...

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="adduniv.php" method="post">
            University Name: <input type="text" name="college">
            No. of branches:
            <div id="dynamicInput">
                 <select name="branches" id="branches" onchange="if (this.selectedIndex) addInput('dynamicInput');;" ">
                     <option value="1">1</option>
                     <option value="2">2</option>
                     <option value="3">3</option>
                     <option value="4">4</option>
                     <option value="5">5</option>
                     <option value="6">6</option>
                     <option value="7">7</option>
                     <option value="8">8</option>
                </select>
            </div>   
            <script>
                var counter = 0;
                var limit = 3;  
                function addInput(divName)
                {       
                    var k=parseInt(document.getElementById("branches"));
                    var newdiv;
                    while(k>0) 
                    {
                        newdiv = document.createElement('div');
                        newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
                        document.getElementById(divName).appendChild(newdiv);
                        counter++;
                        k--;
                     }
                 }
             </script>
             <input type="submit" value="submit" />
         </form>
    </body>
</html>
0

6 Answers 6

1
var k=parseInt(document.getElementById("branches"))

You can't parseInt a DOM element.

Suspect you meant

var k=parseInt(document.getElementById("branches").value,10)

with thanks to the comment from Shikiryu re: specifying the radix explicitly.

Sign up to request clarification or add additional context in comments.

6 Comments

parseInt takes another parameter.
yes, an optional radix parameter which defaults to 10? I don't see the point in including that.
yeah i forgot to add that.. thankyou and i got one more problem.. i cant access the dynamically produced textfield with php.... when javascript is triggered not only textfields but also i cant access whole form with javascript....
@Shikiryu thanks useful info. Of course this case is not going to trigger the octal case, and your link says that modern browsers don't do that anyway. Still no harm in coding defensively.
|
0

document.getElementById("branches") returns a DOM-Node, but what you need to do, is to get the value of this DOM-Node. So try the following to generate k.

var k=parseInt(document.getElementById("branches").value);

1 Comment

yeah i forgot to add that.. thankyou and i got one more problem.. i cant access the dynamically produced textfield with php.... when javascript is triggered not only textfields but also i cant access whole form with javascript.
0

change the line

var k=parseInt(document.getElementById("branches"));

to

var k=parseInt(document.getElementById("branches").value);

1 Comment

yeah i forgot to add that.. thankyou and i got one more problem.. i cant access the dynamically produced textfield with php.... when javascript is triggered not only textfields but also i cant access whole form with javascript.
0

i think you forgot to add .value to document.getElementById("branches")

1 Comment

yeah i forgot to add that.. thankyou and i got one more problem.. i cant access the dynamically produced textfield with php.... when javascript is triggered not only textfields but also i cant access whole form with javascript.
0
parseInt(document.getElementById("branches"));

will result in NaN as far as I can tell. You are trying to parse a whole DOM node as an integer, what did you expect? You might want to get the value property from it:

parseInt(document.getElementById("branches").value, 10);

2 Comments

yeah i forgot to add that.. thankyou and i got one more problem.. i cant access the dynamically produced textfield with php.... when javascript is triggered not only textfields but also i cant access whole form with javascript.
I don't see why not. What are you doing? Are there any errors?
0

Ok, I know the problem was solved, but I would try do this:

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">    
    <title>Creating input fields</title>
    <script>
      function addInput(nr_branches) {
        var nCounter = 0;        
        //-- Cleaning all elements
        var divB = document.getElementById('divBranches');
        if(divB.hasChildNodes()) {
          while(divB.childNodes.length >= 1) {
            divB.removeChild(divB.firstChild);
          }
        }                
        while(nCounter < nr_branches) {          
          //-- Create a input field
          var input = document.createElement("input");
          input.type = 'text';
          input.name = 'branche_nr_' + nCounter;
          input.placeholder = 'Branche Nr.' + nCounter;
          //document.getElementById("divBranches").innerHTML = "<input type='text' name='branche_nr_'"+ nCounter +" placeholder='Branche Nr."+ nCounter +"' />";
          document.getElementById('divBranches').appendChild(input);        
          nCounter++;          
        }                 
      }    
    </script>
  </head>  
  <body>
    <form name="frm" action="adduniv.php" method="post">
      <input type="text" name="college" placeholder="University Name" />
      <select name="branches" id="branches" onchange="addInput(this.value)">
        <option value="0">-select your branche-</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
      </select>
      <div id="divBranches"></div>
      <input type="submit" value="submit" />      
    </form>
  </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.