3
\$\begingroup\$

I've a lot of code blocks that hide/show some block.

$('#Back').on('click', function () {
 $('#FormRow').hide();
 $('#TableRow').show();
});
$('#AddLanguageShow').on('click', function () {
 $('#FormRow').show();
 $("#TableRow").hide();
});
$('table').on('click', 'button.edit', function (evt) {
 $('#FormRow').show();
 $('#TableRow').hide();
 languageManager.load(getRowId($(this)));
});

Is it possible to split logic of changing blocks visibility ?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked May 15, 2014 at 18:14
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Can you provide us more code, it is not clear why you would show or hide stuff right now. \$\endgroup\$ Commented May 15, 2014 at 18:21
  • \$\begingroup\$ Not sure if applicable to your case, but did you consider the toggle() function? api.jquery.com/toggle \$\endgroup\$ Commented May 15, 2014 at 19:09

2 Answers 2

3
\$\begingroup\$

Extract the duplicated code to a function:

function showOneHideOther(elToShow, elToHide) {
 $(elToShow).show();
 $(elToHide).hide();
}

You can reuse the function anyplace now:

$('#Back').on('click', function() {
 showOneHideOther('#TableRow', '#FormRow')
});
$('#AddLanguageShow').on('click', function() {
 showOneHideOther('#FormRow', '#TableRow')
});
$('table').on('click', 'button.edit', function (evt) {
 showOneHideOther('#FormRow', '#TableRow')
 languageManager.load(getRowId($(this)));
});
answered May 15, 2014 at 18:57
\$\endgroup\$
2
  • \$\begingroup\$ I believe this won't work. You're setting undefined to the handlers for #Back and #AddLanguageShow. \$\endgroup\$ Commented May 15, 2014 at 19:02
  • 1
    \$\begingroup\$ You are right @JosephtheDreamer, I already fixed it. Thanks for pointing out my error! \$\endgroup\$ Commented May 15, 2014 at 19:15
1
\$\begingroup\$

It looks like you have one set of interactions that show the form, and another that show the table. How about something like this?

var uiThatHidesTable = [$('#button1'), $('#button3')];
var uiThatHidesForm = [$('#button2')];
var hideTable = function() { $('#TableRow').hide(); $('#FormRow').show(); }
var hideForm = function() { $('#FormRow').hide(); $('#TableRow').show(); }
uiThatHidesTable.forEach(function(el) { el.on('click', hideTable); }); 
uiThatHidesForm.forEach(function(el) { el.on('click', hideForm); });
// you can still bind additional things to do out here 
$('#button2').on('click', function() { alert("pushed 2"); });

Fiddle here: http://jsfiddle.net/T7swj/

answered May 15, 2014 at 19:09
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.