Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript add fields dynamically

//Add more fields dynamically.
function addField(field,area,limit) {
	if(!document.getElementById) return; //Prevent older browsers from getting any further.
	var field_area = document.getElementById(area);
	var all_inputs = field_area.getElementsByTagName("input"); //Get all the input fields in the given area.
	//Find the count of the last element of the list. It will be in the format '<field><number>'. If the 
	//		field given in the argument is 'friend_' the last id will be 'friend_4'.
	var last_item = all_inputs.length - 1;
	var last = all_inputs[last_item].id;
	var count = Number(last.split("_")[1]) + 1;
	
	//If the maximum number of elements have been reached, exit the function.
	//		If the given limit is lower than 0, infinite number of fields can be created.
	if(count > limit && limit > 0) return;
 	
	if(document.createElement) { //W3C Dom method.
		var li = document.createElement("li");
		var input = document.createElement("input");
		input.id = field+count;
		input.name = field+count;
		input.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc.
		li.appendChild(input);
		field_area.appendChild(li);
	} else { //Older Method
		field_area.innerHTML += "<li><input name='"+(field+count)+"' id='"+(field+count)+"' type='text' /></li>";
	}
}
Comment

java script add fields dynamically

<form name="frm" method="POST">
<strong>Friends List</strong><br />
<ol id="friends_area">
<li><input type="text" name="friend_1" id="friend_1" /></li>
<li><input type="text" name="friend_2" id="friend_2" /></li>
<li><input type="text" name="friend_3" id="friend_3" /></li>
<li><input type="text" name="friend_4" id="friend_4" /></li>
<li><input type="text" name="friend_5" id="friend_5" /></li>
</ol>
<input type="button" value="Add Friend Field" onclick="addField('friends_area','friend_',10);" />

<strong>Enemies List</strong><br />
<ol id="enemies_area">
<li><input type="text" name="enemy_1" id="enemy_1" /></li>
<li><input type="text" name="enemy_2" id="enemy_2" /></li>
<li><input type="text" name="enemy_3" id="enemy_3" /></li>
<li><input type="text" name="enemy_4" id="enemy_4" /></li>
<li><input type="text" name="enemy_5" id="enemy_5" /></li>
</ol>
<input type="button" value="Add Friend Field" onclick="addField('enemies_area','enemy_',0);" />
</form>
Comment

javascript add fields dynamically

//Add more fields dynamically.
function addField(field,area,limit) {
	if(!document.getElementById) return; //Prevent older browsers from getting any further.
	var field_area = document.getElementById(area);
	var all_inputs = field_area.getElementsByTagName("input"); //Get all the input fields in the given area.
	//Find the count of the last element of the list. It will be in the format '<field><number>'. If the 
	//		field given in the argument is 'friend_' the last id will be 'friend_4'.
	var last_item = all_inputs.length - 1;
	var last = all_inputs[last_item].id;
	var count = Number(last.split("_")[1]) + 1;
	
	//If the maximum number of elements have been reached, exit the function.
	//		If the given limit is lower than 0, infinite number of fields can be created.
	if(count > limit && limit > 0) return;
 	
	if(document.createElement) { //W3C Dom method.
		var li = document.createElement("li");
		var input = document.createElement("input");
		input.id = field+count;
		input.name = field+count;
		input.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc.
		li.appendChild(input);
		field_area.appendChild(li);
	} else { //Older Method
		field_area.innerHTML += "<li><input name='"+(field+count)+"' id='"+(field+count)+"' type='text' /></li>";
	}
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: import and export data in mongodb 
Javascript :: vscode format - .prettierrc jsx singleQuote not work 
Javascript :: use different environment variables in production and development 
Javascript :: prevent a function from being called too many times react 
Javascript :: interactive svg javascript 
Javascript :: Remove an item by index position 
Javascript :: react useeffect hooks 
Javascript :: this.$set in vue 3 
Javascript :: GET and CHANGE the class of an element 
Javascript :: react native comment in render 
Javascript :: js string insert space 
Javascript :: react-multi-carousel equal spacing issue 
Javascript :: react native ant design 
Javascript :: how to find remainder in javascript 
Javascript :: javascript string length 
Javascript :: jquery datepicker enable year selection 
Javascript :: next js get query parameters 
Javascript :: jquery alertify 
Javascript :: vue method 
Javascript :: Jest DOM Manipulation 
Javascript :: puppeteer stealth popup 
Javascript :: javascript array destructuring 
Javascript :: temporal dead zone in es6 
Javascript :: delete in array 
Javascript :: jquery in javascript 
Javascript :: await the end of subscribe angular 
Javascript :: how to start react project on atom 
Javascript :: routes in angular 
Javascript :: unknown provider angularjs 
Javascript :: material-ui.com autocomplete grouped 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =