How do I store the <TR> id value in an array when I switch?
I switch 'tr.subCategory1'
both his brothers and sisters .RegText
. at the same time I am trying to store its ids in an array like this one list_Visible_Ids[$(this).attr('id')] = $(this).css('display') != 'none' ? 1 : null;
(When I crashed I need to store 'null'
in the array in its id location. If I expand I need a store. I need to store 1
in its id location). But it alert($(this).css('display'))
shows every time block
. How can I handle this ?. So when I collapse or expand, it only saves 1
.
$(document).ready(function() {
$('tr[@class^=RegText]').hide().children('td');
list_Visible_Ids = [];
var idsString, idsArray;
idsString = $('#myVisibleRows').val();
idsArray = idsString.split(',');
$.each(idsArray, function() {
if (this != "" || this != null) {
$('#' + this).siblings('.RegText').toggle();
list_Visible_Ids[this] = 1;
}
});
$('tr.subCategory1')
.css("cursor", "pointer")
.attr("title", "Click to expand/collapse")
.click(function() {
$(this).siblings('.RegText').toggle();
$(this).siblings('.VolumeRegText').toggle();
//alert($(this).css('display'))
list_Visible_Ids[$(this).attr('id')] = $(this).css('display') != 'none' ? 1 : null;
});
$('#form1').submit(function() {
idsString = '';
for (var index in list_Visible_Ids) {
idsString += (idsString != '' ? ',' : '') + index;
}
$('#myVisibleRows').val(idsString);
form1.submit();
});
});
a source to share
You do not switch yourself tr
, but only his brothers and sisters (with class .RegText
and .VolumeRegText
). Therefore, you should check if they are visible when you store the state in the array. For this you can use .is(":hidden")
for one of the brothers and sisters. Then the click function would look like this:
$('tr.subCategory1')
.css("cursor", "pointer")
.attr("title", "Click to expand/collapse")
.click(function() {
$(this).siblings('.RegText').toggle();
var isHidden = $(this).siblings('.VolumeRegText').toggle().is(':hidden');
list_Visible_Ids[$(this).attr('id')] = !isHidden ? 1 : null;
});
There are also many comments on the rest of the code. AT
$('tr[@class^=RegText]').hide().children('td');
skip .children('td')
as you are not using this selection.
list_Visible_Ids = [];
should be declared with var
.
By looping through
idsArray
, you check
this != "" || this != null)
which should be used
&&
instead
||
.
$.each(idsArray, function() {
if (this != "" && this != null) {
$('#' + this).siblings('.RegText').toggle();
list_Visible_Ids[this] = 1;
}
});
It also makes no sense to use a function $.each()
instead of a regular JavaScript for-loop.
It also appears that you are using an older version of jQuery, as use
@
in selectors was deprecated in version 1.3 .
a source to share