I am looping through the body of an html table and I want to capture the position of the ( td ) where the selects are selected, this happens in a jquery click event . I have the following:
//creo el objeto
var objetoSelects = new Object();
$('#vistaPre tbody tr').each(function (index) {
$(this).find("td").each(function(i, el) {
// .selectOptions son selects que tengo en un td de la tabla
$('.selectOptions').find(':selected').each(function(j, el) {
$this = $(this).val();
if ($this != '') {
// intento crear las posiciones del objeto
objetoSelects.$this = j;
objetoSelects.selects = $this;
}
});
// salgo del each
return false;
});
// salgo del each
return false;
});
My intention is to create an object like this:
// $this es el valor del select ejemplo ('item1')
// j es el contador en este caso la posición actual del <td>
// selects es el valor que le deseo dar a mi otro dato (creo que no esta bien)
objetoSelects(itemx = x, 'selects' = itemx);
I can't find a way to do it.
UPDATE
Based on the answers, I found a way to do it, below is the way I solved the problem
// cree arrays para guardar la posición del <td> y el valor del select
var arraySelects = [];
var arrayPosition = [];
$('#vistaPre tbody tr').each(function (index2) {
$(this).find("td").each(function(i, el) {
//
$('.selectOptions').find(':selected').each(function(j, el) {
$this = $(this).val();
if ($this != '') {
//agrego los datos correspondientes
arrayPosition.push(j);
arraySelects.push($this);
}
});
return false;
});
return false;
});
// creo el objeto y le asigno los valores
var datoSelect = { 'position': arrayPosition, 'selects': arraySelects};
Alan, here is an example of what you are looking for. Native JS script without external libraries.
I do not understand very well your question.
you create an object but from the name I assume you want an array?
if you definitely want an object then you can do:
Let's remember that
arraySelects
you created it as an object, after finishing that call, you can queryarraySelects['key']
replacingkey
it with the name you gave it when you were saving it.You can assign values directly using the syntax to create objects:
objetoSelects = {"items": x, "select": itemx}