\$\begingroup\$
\$\endgroup\$
It seems it could be written shorter. It's especially annoying when I have to do this in multiple languages, so the button labels will be different.
<button id="egyes" class="btn btn-danger">Hide</button> some text
<button id="kettes" class="btn btn-warning">Hide</button> other text
<button id="harmas" class="btn btn-info">Hide</button> some more text
<button id="otos" class="btn btn-success">Hide</button> last button
<button id="showall" class="btn">Show all</button>
<script>
$(document).ready(function() {
$("#egyes").click( function() {
$("div.progress-danger").parent().fadeToggle();
if ($(this).html() == "Show") {
$(this).html("Hide");
} else {
$(this).html("Show");
}
});
$("#kettes").click( function() {
$("div.progress-warning").parent().fadeToggle();
if ($(this).html() == "Show") {
$(this).html("Hide");
} else {
$(this).html("Show");
}
});
$("#harmas").click( function() {
$("div.progress-info").parent().fadeToggle();
if ($(this).html() == "Show") {
$(this).html("Hide");
} else {
$(this).html("Show");
}
});
$("#otos").click( function() {
$("div.progress-success").parent().fadeToggle();
if ($(this).html() == "Show") {
$(this).html("Hide");
} else {
$(this).html("Show");
}
});
$("#showall").click( function() {
$("div.container > div > div.progress").parent().fadeIn();
});
});
</script>
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 14, 2012 at 5:44
1 Answer 1
\$\begingroup\$
\$\endgroup\$
4
if you add the parent div class into the data attributes:
<button id="egyes" class="btn btn-danger" data-parentclass="progress-danger">Hide</button> some text
<button id="kettes" class="btn btn-warning" data-parentclass="progress-warning">Hide</button> other text
<button id="harmas" class="btn btn-info" data-parentclass="progress-info">Hide</button> some more text
<button id="otos" class="btn btn-success" data-parentclass="progress-success">Hide</button> last button
<button id="showall" class="btn">Show all</button>
or add it in via javascript:
$("#egyes").data("parentclass", "progress-danger");
// etc
then the javascript can work it out itself.
$(document).ready(function() {
$("#egyes, #kettes, #harmas, #otos").click( function() {
var $parent = $("div." $(this).data("parentclass")).parent();
if ($parent.is(":visible")) {
$(this).html("Hide");
} else {
$(this).html("Show");
}
parent.fadeToggle();
});
$("#showall").click( function() {
$("div.container > div > div.progress").parent().fadeIn();
});
});
answered Aug 14, 2012 at 6:51
-
\$\begingroup\$ definately looks better ! \$\endgroup\$kissgyorgy– kissgyorgy2012年08月14日 07:38:14 +00:00Commented Aug 14, 2012 at 7:38
-
\$\begingroup\$ however
parent.is(":visible")
will be always true, because when the script reach theif(...)
the divs will be already visible ! \$\endgroup\$kissgyorgy– kissgyorgy2012年08月14日 08:05:07 +00:00Commented Aug 14, 2012 at 8:05 -
\$\begingroup\$ @Walkman whoops! ... wasn't able to test it. Just move the
.fadeToggle
after it? ... I can't actually test it without some more markup. (or maybe a jsfiddle.net?) \$\endgroup\$James Khoury– James Khoury2012年08月14日 09:30:46 +00:00Commented Aug 14, 2012 at 9:30 -
1\$\begingroup\$ I tested it and worked after the modification. \$\endgroup\$kissgyorgy– kissgyorgy2012年08月14日 09:37:13 +00:00Commented Aug 14, 2012 at 9:37
default