1
I would like to know how I can complement the code below to have how to return values in checkbox mode the problem and that I need this check to be performed in the following way keep the array position created at the time of marking the items following the order of values shown in the same position within the textarea as well as the number that appears on the side of the marked items showing the positions in which they were selected.
The reason for this is that I have two areas one that sends the checkbox values to the database and the other the edit mode that returns the values contained within the database, the problem is that as this code generates array position for the marked items when returning the selected values contained within the database this script is not returning the positions of the array of items in the check mode and even putting checkbox values not inserts these values within the textarea as well as their markup positions created when you have an item marked in the checkbox.
var listCheckedOptions = [];
function addToList(checkObj, outputObjID) {
//Remove do array caso o elemento já esteja inserido
if (listCheckedOptions.indexOf(checkObj) >= 0) {
listCheckedOptions.splice(listCheckedOptions.indexOf(checkObj), 1);
} else {
if (listCheckedOptions.length >= 2) {
alert("Máx 2 Elemenos selecionados");
return checkObj.checked = false;
}
listCheckedOptions.push(checkObj);
}
document.getElementById(outputObjID).value = ""; //Limpa o textarea
document.getElementById(outputObjID).value = listCheckedOptions.map(function (o) {
return o.value;
}).join('\r\n'); //Adiciona no textarea
if (!checkObj.checked) checkObj.nextSibling.querySelector('span').innerHTML = '';
listCheckedOptions.forEach(function (el, i) {
var span = el.nextSibling.querySelector('span').innerHTML = i + 1;
});
return;
}
<form name="myform">
<input type="checkbox" name="fruit[]" value="Oranges" onClick="addToList(this, 'txt1')"><font color="#808080">Oranges <span></span></font>
<br>
<input type="checkbox" name="fruit[]" value="Apples" onClick="addToList(this, 'txt1')"><font color="#808080">Apples <span></span></font>
<br>
<input type="checkbox" name="fruit[]" value="Grapes" onClick="addToList(this, 'txt1')"><font color="#808080">Grapes <span></span></font>
<br>
<textarea rows="4" cols="10" name="txt1" id="txt1" style="color:#808080" readonly></textarea>
</form>
You can write an example of whatever appears inside the textarea?
– KaduAmaral
Just look at the code currently when you mark an item it first when checking sends the value at the position it was selected for the textarea then puts a number indicating the order in which it was selected right now if you put a checked checbox with is only marked and not emits neither the selection value for textarea nor even its position does not show the number on the side of the text showing the order in which they were marked need that in checked it does the same.
– Striffer
I created a snippet with your example, I could see that the textarea is not in the same order in the checkbox, this is your problem?
– Tobias Mesquita
Rodrigo, the tag entry is not appearing after the checkbox, because there is no <span> tag in html, possibly you forgot to add them.
– Tobias Mesquita
As for the initial value in textarea, I believe you will have to scan the checkbox on onload.
– Tobias Mesquita
That would be Rodrigo: http://jsfiddle.net/dsy0amLf/1/
– KaduAmaral
Your jsfiddle just didn’t work out for one detail. all of their methods were declared in the onready of jQuery, so they were not visible to the DOM, I only had to make a small change to correct: http://jsfiddle.net/dsy0amLf/2/
– Tobias Mesquita