Jump to content

[SOLVED] Creating New input box.


UpcomingPhpDev

Recommended Posts

Hey guys, I have this code below, that is working fine, But When the user pressed add field, Id like it to create 2 new fields.

 

Ive tried editing it, But as my knowledge of Js/DOM isnt very good, I cant seem to do it.

 

Thanks in advance!

 

<form method="post">
<p id="parah">Click below to dynamically create/remove input boxes in this field</p>

<a href="javascript:addInput()">Add Field</a><br>
<a href="javascript:deleteInput()">Remove Field</a>
<input type="submit" />
</form>

<script type="text/javascript">
var arrInput = new Array(0);
  var arrInputValue = new Array(0);

function addInput() {
  arrInput.push(arrInput.length);
  arrInputValue.push("");
  display();
}

function display() {
  document.getElementById('parah').innerHTML="";
  for (intI=0;intI<arrInput.length;intI++) {
    document.getElementById('parah').innerHTML+=createInput(arrInput[intI], arrInputValue[intI]);
  }
}

function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
}  

function createInput(id,value) {
  return "<input name='field["+ value +"][]' id='test "+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'><br>";
}

function deleteInput() {
  if (arrInput.length > 0) { 
     arrInput.pop(); 
     arrInputValue.pop();
  }
  display(); 
}
</script>

Link to comment
https://forums.phpfreaks.com/topic/139034-solved-creating-new-input-box/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.