0
How can I change the script below to work in the following precise way that it performs the following limit the number of marks or can select in the max 2 items of the list if it exceeds the maximum allowed value of an alert message.
<html>
<head>
<title>Seleção de Itens</title>
<script type="text/javascript">
//Array que guarda a ordem em que os elementos foram inseridos
var listCheckedOptions = [];
function addToList(checkObj, outputObjID)
{
//Remove do array caso o elemento já esteja inserido
if (listCheckedOptions.indexOf(checkObj.value) >= 0) {
listCheckedOptions.splice(listCheckedOptions.indexOf(checkObj.value), 1);
} else { //Adiciona casojá esteja inserido
listCheckedOptions.push(checkObj.value);
}
// alert(listCheckedOptions); //debug para verificar os elementos inseridos
document.getElementById(outputObjID).value = ""; //Limpa o textarea
document.getElementById(outputObjID).value = listCheckedOptions.join('\r\n'); //Adiciona no textarea
return;
}
</script>
</head>
<body>
<form name="myform">
<input type="checkbox" name="fruit[]" value="Oranges" onClick="addToList(this, 'txt1')"><font color="#808080">Oranges</font><br>
<input type="checkbox" name="fruit[]" value="Apples" onClick="addToList(this, 'txt1')"><font color="#808080">Apples</font><br>
<input type="checkbox" name="fruit[]" value="Grapes" onClick="addToList(this, 'txt1')"><font color="#808080">Grapes</font><br>
<textarea rows="4" cols="10" name="txt1" id="txt1" style="color:#808080" readonly></textarea>
</form>
</body>
</html>
That’s what I wanted to thank you for your help.
– Striffer
Only a complement as I can in the case type when checking the item in the checkbox make appear a number in check order because in the case instead of textarea I will be using hidden field have to make numbers appear in the checkbox items when they mark appear at positions where they were marked in case of selecting the second take the position of the first and so on ?
– Striffer
When so @Rodrigo, you can and must create a new question, because this new doubt runs too far from the context of the original question, there is no problem in this case
– MarceloBoni
Grateful for the answer I am then asking a new question with the code obtained from this answer.
– Striffer