0

I need help to consolidate/minify the code below. Originally I had a dropdown which controls div's visibility based on value. It works onClick but since it's a database page, it doesn't work onLoad so I had to add a 2nd logic. Any suggestions/input is greatly appreciated.

$(document).ready(function () {
  var $caseStatus = $(this).find("#auth_status").val();
  //alert ($caseStatus);
  if ($caseStatus === "1") {
    $(".pendingClass").show();
    $(".authClass").hide();
  }
  else if ($caseStatus === "2") {
    $(".pendingClass").hide();
    $(".authClass").show();
  }
  else {
    $(".pendingClass").hide();
    $(".authClass").hide();
  }

  // Show/Hide Divs for Authorization Status
  $("#auth_status").click(function () {
    if ($(this).val() === "0") {
      $(".pendingClass").hide();
      $(".authClass").hide();
    }
    else if ($(this).val() === "1") {
      $(".pendingClass").show();
      $(".authClass").hide();
    }
    else if ($(this).val() === "2") {
      $(".pendingClass").hide();
      $(".authClass").show();
    }
  });
});
0

3 Answers 3

2

You could reduce your code a bit by simplifying your if/else structure(since anything besides "1" is the same show/hide result and also just call the handler you're binding when the page first loads, like this:

$(function () {
  $("#auth_status").click(function () {
    var val = $(this).val();
    $(".pendingClass").toggle(val === "1");
    $(".authClass").toggle(val === "2");
  }).click();
});
Sign up to request clarification or add additional context in comments.

8 Comments

+1 That's more elegant than my version. But the $caseStatus should be named caseStatus, actually. ;-) And the $(this).val() === "1"; assumption is too simplified, since there are three cases.
@Tomalak - Better? :) I'm not sure what 1 translates to for statuses, but it should be named that status really, to be as clear as possible.
@Tomalak - Woops, I read that 0 and 2 were the same, you're right those show/hides are different, updated with the correction.
@Nick Craver: That's it. The thought to express it as click(function …).click() occurred to me a little too late, and now I don't want to change my answer to avoid looking like a copy cat. ;-)
@Tomalak - fair enough, +1 from me though :)
|
1
$(function () {
  function toggleElements() {
    var caseStatus = $("#auth_status").val();
    $(".pendingClass").toggle(caseStatus === "1");
    $(".authClass").toggle(caseStatus === "2");
  };

  toggleElements();                         // initial call (on page load)
  $("#auth_status").click(toggleElements);  // manual call (on user click)
});

Comments

0

You could define a couple of objects:

var authCtrl = {
  '0': { show: null, hide: '.pendingClass, .authClass' },
  '1': { show: '.pendingClass', hide: '.authClass' },
  '2': { show: '.authClass', hide: '.pendingClass' }
};

and similarly (backwards) for the other case. Then the handler just grabs the entry based on that field value and hides/shows as directed.

9 Comments

Isn't this a bit overkill? :)
Well it depends. Encoding the behavior as data like this decouples the "rules" from the logic that acts on the rules. A handler written to interpret plain data like that (maybe even more generalized) can potentially be used for many similar structures that differ only in some particulars. I'm trying to teach myself Category Theory so I'm lately in the habit of thinking like this :-)
@Nick: It would enable you to do $(#auth_status").change(function () { $.each(authCtrl[$(this).val()], function(k, v) { $(v)[k](); }); }); :-D (But only when all possible values of #auth_status are in authCtrl…)
@Tomalak if the combinatorial problem gets big, then a much better approach would be to adjust the markup to be more cooperative.
One approach would be to encode the "states" into the "behavior class" objects, instead of using just "show" and "hide". Since I have no clue how the page actually works, of course, it's hard to come up with the best answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.