Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collapse fieldset when legend element clicked

Tags:

jquery

toggle

I have a jQuery function which toggles the visibility of the contents of a fieldset when its legend is clicked, leaving just the fieldset border (if there is one) and the legend showing:

$('legend.togvis').click(function() {
    $(this).siblings().toggle();
    return false;
});

It works great unless the fieldset contains text nodes.

<fieldset><legend class='togvis'>Click Me</legend>
  <p>I toggle when the legend is clicked.</p>
  But I'm a recalcitrant text node and refuse to toggle.
</fieldset>

In an effort to toggle the text nodes too I tried this:

$('legend.togvis').click(function() {
    $(this).parent().contents().not('legend').toggle();
    return false;
});

which works the same as the first function. And this:

$('legend.togvis').click(function() {
    $(this).parent().contents(":not(legend)").toggle();
    return false;
});

which throws the error

Message: 'style.display' is null or not an object
Line: 5557
Char: 3
Code: 0
URI: http://code.jquery.com/jquery-1.4.4.js

Any thoughts on how to get the entire contents of a fieldset (minus the legend) to toggle when the legend is clicked?

ETA Solution, with many thanks to Eibx

$('legend.togvis').click(function() { 
    $(this).parent().contents().filter(
        function() {  return this.nodeType == 3; }).wrap('<span></span>');//wrap any stray text nodes
    $(this).siblings().toggle(); 
}); 
like image 844
dnagirl Avatar asked Jan 11 '11 14:01

dnagirl


People also ask

Can I use Fieldset without legend?

Every fieldset element must contain exactly one legend element. Multiple legend elements contained in the same fieldset may result in the improper calculation of labels for assistive technologies.

What do Fieldset and Legend elements do?

These elements work together to tell screen readers that a group of form fields relate to each other, and to provide a label for the group. All of the related fields go inside the <fieldset> element, and the <legend> element is used to represent the question instead of the <p> element.

What does a legend define for a Fieldset?

<legend>: The Field Set Legend element The <legend> HTML element represents a caption for the content of its parent <fieldset> .

What does Fieldset mean in HTML?

<fieldset>: The Field Set element. The <fieldset> HTML element is used to group several controls as well as labels ( <label> ) within a web form.


2 Answers

You simply cannot make text disappear in HTML, JavaScript or CSS. It needs to be contained in a HTML element that will have display:none; applied or something similar.

The following code will wrap everything into a div, and move your legend to the same level as the div, and make the div show/hide when you click on the legend.

$("fieldset legend").click(function() {
  if ($(this).parent().children().length == 2)
    $(this).parent().find("div").toggle();
  else
  {
    $(this).parent().wrapInner("<div>");
    $(this).appendTo($(this).parent().parent());
    $(this).parent().find("div").toggle();
  }
});
  • Code: http://jsbin.com/eleva3/edit
  • Preview: http://jsbin.com/eleva3
like image 162
はると Avatar answered Sep 27 '22 18:09

はると


Since text nodes are not elements, there is no way to toggle them.

As a last resort, if you cannot wrap them with an element, you can remove everything except the legend and add the elements and the text nodes back later:

$(document).ready(function() {
    $('legend.togvis').click(function() {
        var $this = $(this);
        var parent = $this.parent();
        var contents = parent.contents().not(this);
        if (contents.length > 0) {
            $this.data("contents", contents.remove());
        } else {
            $this.data("contents").appendTo(parent);
        }
        return false;
    });
});

You can test that solution here.

like image 27
Frédéric Hamidi Avatar answered Sep 27 '22 17:09

Frédéric Hamidi