Ciao a tutti su internet ho trovato un bello scrippettino che faceva al caso mio, solo che vorrei apportargli una modifica:
Codice originale:
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"></meta>
<title></title>
<script language="Javascript" type="text/javascript">
<!--
//Add more fields dynamically.
function addField(area,field,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 = 'campo[]';//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 += "[*]<input name='campo[]' id='"+(field+count)+"' type='text' />";
}
}
//-->
</script>
</head>
<body>
<form method="POST" action="" name="formsondaggio">
- <input name="campo[]" id="fields_1" type="text" />
<a onclick="this.parentNode.parentNode.removeChild(this.parentNode);">
Remove Field
</a>
- <input name="campo[]" id="fields_1" type="text" />
<a onclick="this.parentNode.parentNode.removeChild(this.parentNode);">
Remove Field
</a>
- <input name="campo[]" id="fields_1" type="text" />
<a onclick="this.parentNode.parentNode.removeChild(this.parentNode);">
Remove Field
</a>
<input type="submit" name="invio" /><input type="reset" name="reset" />
<input value="Add Field" onclick="addField('pollfields','fields_',0);" type="button" />
</form>
</body>
</html>
Vorrei modificarlo in modo da ottenere anche affianco ai nuovi campi aggiunti, la scritta remove field, perchè lo vedo più usabile in questo modo.
Da quel poco che so di javascript la modifica andrebbe dopo
codice:
if(document.createElement)
{
//qui inserire modifica
}else{...}
ma cosa ci si deve mettere??
ho provato una modifica rozza giusto per vedere se funzionava, ovvero lasciando solo questa stringa
codice:
field_area.innerHTML += "[*]<input name='campo[]' id='"+(field+count)+"' type='text' /><a onclick='this.parentNode.parentNode.removeChild(this.parentNode);'>Remove Field</a>";
senza l'else e tutto funziona, ma volevo capire a cosa serve l'if con i suoi elementi dentro e se c'è un modo per non cancellare quella parte di codice.
spero di essermi fatto capire
e soprattutto spero possiate aiutarmi a chiarirmi un po' le idee.
Grazie Ciao