And that code works perfectly.
But I wish add a changing icon (“+” when collapsed, “-” when expanded) after the expansible link, modifying the less possible the jquery code and the html code.
Any idea?
Thanks!
function expand(divId) {
// just so only one DOM search has to be done....
var div = $("#" + divId);
// toggle visibility
div.toggle();
// show appropriate +/- depending on visibility of div.
$(".internal").html(div.is(":visible") ? "-" : "+");
}
Thank you. I prefer the code of @DaveMaxwell : the simpler, the better.
But there are some further improvements. I forgot to say that so far I used already an icon, but a static one, with this css:
a[href^="javascript"]:after {
content: '\002B'
}
Moreover the code of @Davereplace (after clicking, with a “+”) the text I can put between the <a>...</a>, and this is a problem.
Uhm… but in my website there are several IDs to working with, not only one (#name), as in your (working) example.
Therefore your code, as is, doesn’t work in my website (and, I guess, in any website with several IDs to expand) . Maybe with some fix… I guess, using a class, rather than an ID.
Between <a> and </a> there is a text, semantically consistent, not deceiving.
a are anchors for links to go somewhere. It could be to another portion of the same page, or somewhere entirely different. But it implies a destination.
That’s not what you’re doing here. What you’re doing is more consistent with a button more so than a link, if anything. But even a button implies an actionable form is going to operate.
Actually, a checkbox is probably more semantically correct than anything because you’re toggling a value. You can use the value and some JS to dictate what element to show/hide
OK, this time it works, but I have a problem with the style of the expanded text.
I wish it be
{
display: block;
color: navy;
font-size: 85%;
}
Instead, now is a “normal” (as color and size) style, displayed as inline.
How should I do?
Thank you.
Maybe you are right, but the change I should do is too big (I have hundred of this type of expansible link).
Besides an <input> tag seems to me even less semantic
I have to do some new attempts, but it seems quite correct. Thank you!
The main problem is that the display:block. If setted the invisible text becomes always visible (expanded).
Therefore, so far, I have to accept an inline display. I’d like something like display:block …