Performing checkbox checking with jquery

Asked

Viewed 339 times

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?

  • 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.

  • 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?

  • Rodrigo, the tag entry is not appearing after the checkbox, because there is no <span> tag in html, possibly you forgot to add them.

  • As for the initial value in textarea, I believe you will have to scan the checkbox on onload.

  • That would be Rodrigo: http://jsfiddle.net/dsy0amLf/1/

  • 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/

Show 2 more comments

1 answer

2


see if the code below is what you want.

var frutas = document.getElementsByName("fruit[]");
var txt1 = document.getElementById("txt1");

frutas = [].slice.apply(frutas);
var listCheckedOptions = frutas.filter(function (fruta, indice) {
    return fruta.checked;
}).sort(function (fruta, indice) {
    return fruta.dataset.order;
});


function addToList(checkObj, outputObj) {
  //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);
  }
  
  if (!checkObj.checked) {
    checkObj.parentNode.querySelector('span').innerHTML = '';  
    delete checkObj.dataset.order;
  }
  return updateValores(outputObj);
}

var updateValores = function (outputObj) {
  outputObj.value = ""; //Limpa o textarea
  outputObj.value = listCheckedOptions.map(function (o) {
    return o.value;
  }).join('\r\n'); //Adiciona no textarea  
  
  listCheckedOptions.forEach(function (fruta, indice) {
    var span = fruta.parentNode.querySelector('span');
    fruta.dataset.order = indice + 1;
    span.innerHTML = indice + 1;
  });

  return;
}

frutas.forEach(function (fruta, indice) {
  fruta.onclick = function () {
    addToList(this, txt1);
  };
});
updateValores(txt1);
<form name="myform">  
  <label>
    <input type="checkbox" name="fruit[]" value="Oranges" data-order="2" checked />  
    <font color="#808080">Oranges</font>
    <span></span>
  </label><br>
  <label>
    <input type="checkbox" name="fruit[]" value="Apples" />
    <font color="#808080">Apples</font>
    <span></span>
  </label><br>
  <label>
    <input type="checkbox" name="fruit[]" value="Grapes" data-order="1" checked />
    <font color="#808080">Grapes</font>
    <span></span>
  </label><br>
  <textarea rows="4" cols="10" name="txt1" id="txt1" style="color:#808080"  readonly></textarea>
</form>

  • Only one thing I need besides what you have already done and in case the array is returning the check values in the order of the right listing only what type if I have in that order no if I have the items marked in the database in this order the values first Grapes after Oranges as I can make Grapes 1 and Oranges 2, in the complement you made.

  • I updated the example, in this case I advise using a data-custom property to store the order coming from the bank. you can even update the value of this property, like the above code, but this will not affect the result, therefore being optional.

  • Thank you for the help now yes it’s all the style I need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.