views:

67

answers:

2

Afternoon,

I have set up the following, the nested ifs are working however when i'd like to be able to pass the title of the array to the alterFields() function, that way the function will cycle through each field stored in the array and apply the CSS class relative to that array (i.e. I have applied the same name to the array as the css class). Unfortunately if I use strings in the css array the $.each in alterfields cycles through each character of the string.

var assigned =[
"UAT Nominee"
];

var applications = [
"Primary Application Affected",
"Other Applications"
];

var comments = [
"Comments"
]

var css = [assigned, applications, comments];

    $.each(css, function(x){
    var current_class = css[x];
    alterfields(current_class);
    });

    function alterfields(array){
        $.each(array, function(i){
        var current_field = array[i];
    alert(current_field);
            $("#WebPartWPQ2 .ms-formlabel nobr").filter(function() {
                return $.text([this]) === array[i];
                }).closest('tr').toggleClass(array);
            });
        }

});

Thanks in Advance

+1  A: 

Try something like this instead.

var fields = {
  assigned: [ "UAT Nominee" ],
  applications: [ "Primary Application Affected", "Other Applications" ],
  comments: [ "Comments" ]
};

var field_keys = [ 'assigned', 'applications', 'comments'];

$.each(field_keys, function(){
  alterfields( this );
});

function alterfields( field_key ){
  $.each(fields[ field_key ], function(){
    var current_field = this;
    $("#WebPartWPQ2 .ms-formlabel nobr").filter(function() {
      return $(this).text() === current_field;
    }).closest('tr').toggleClass( field_key );
  });
}
BBonifield
Hi BBonified, apologies for coming back to you. Unfortunately I can't get it all to work, its definately picking up the correct values and cycling through the arrays but doesnt seem to be applying any css class. Sorry Here's my code can you spot anything?
Gary
<script>$(document).ready(function(){var fields = {releasedetails: ["Year-Mon", "Release Date"],changedetails: ["Original Planned Release Date (Business Systems)", "Change submission date", "Priority", "TP Problem Score", "UAT Nominee"],progress: ["Status"],assigned: ["Business Analyst", "Development Owner"]};var field_keys = ['releasedetails', 'changedetails', 'progress', 'assigned'];
Gary
$.each(field_keys, function(){ alterfields( this ); }); function alterfields( field_key ){ $.each(fields[ field_key ], function(){ var current_field = this; //alert(current_field + "\n" + field_key); $("#WebPartWPQ2 .ms-formlabel nobr").filter(function() { return $(this).text() === current_field; }).closest('tr').removeClass('hidden').toggleClass( field_key ); }); }
Gary
A: 

What you're doing isn't quite right. this line:

var css = [assigned, applications, comments];

makes CSS an numeric array of those numeric arrays. To reference those objects, you'll have to use css[0], css[1], etc. I do believe you want to exploit the fact that javascript objects can be treated as named arrays. so you would do something like this:

var css = {
  "assigned": assigned,
  "applications": applications,
  "comments": comments
};

Then when you reference css["assigned"] you get your numeric array that you're expecting.

Richard June