Problems with Javascript (AddInput) -


<html> <head> <title></title> <script language="javascript"> fields = 0; function addinput() { if (fields != 3) { document.getelementbyid('text').innerhtml += "<input type='text' value='' /><br />"; fields += 1; } else { document.getelementbyid('text').innerhtml += "<br />only 3 fields allowed."; document.form.add.disabled=true; } } </script> </head> <body> <form name="form"> <input type="button" onclick="addinput()" name="add" value="add input field" /> </form> </body> </html> 

the question if submit code, how can know name each input text had been added.

thanks helping, i'm newbies in js.

you assign them id's , names when create them in javascript code. following example gives each input ids , names "field1", "field2" , "field3" when created:

<script language="javascript">     fields = 0;     function addinput() {         if (fields != 3) {             fields += 1;             document.getelementbyid('text').innerhtml += "<input id='field" + fields + "' name='field" + fields + "' type='text' value='' /><br />";         } else {             document.getelementbyid('text').innerhtml += "<br />only 3 fields allowed.";             document.form.add.disabled=true;         }     } </script> 

then, can either call them name like:

var myfirstfield = document.getelementbyid('field1'); 

and on postback, can reference names if form submitted page.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -