I am rewriting old JavaScript with jQuery and would like to know how to write it more cleanly.
script I start with:
for (var i = 0; i < form1.elements.length; i++) {
var element = form1.elements[i];
alert(element.id)
if (Left(element.id, 15) === 'selHeaderFilter' ||
element.id === 'ddlHierarchy1') {
garrHeaderState[element.id] = element.selectedIndex
}
}
I am not sure why this was written as it was, but I think it is just trying to add 2 elements to an array (declared globally).
My first blow on this occasion gave me:
var hierarchy = $('[id$=ddlHierarchy1]');
var headerFilter = $('[id*="selHeaderFilter"]');
if (hierarchy)
garrHeaderState[hierarchy.attr('id')] = hierarchy.val();
if (headerFilter)
garrHeaderState[headerFilter.attr('id')] = headerFilter.val();
But I don't really like the process of declaring a hierarchy, so if it exists, I can add my highlighted index to the array. Is there a better way to write this code?
source
share