0

Follow-up on: How to get the children of the $(this) selector?

Does not work for me somehow.

http://jsfiddle.net/teKwN/1/

html

  <h4>
    Sometext
    <h3>
      Another text
    </h3>
  </h4>

css

h3 {
    display: none;
}

js

$('h4').live('click', function() {
    $(this).children('h3').toggle('slide');
});

EDIT: There are multiple items like h4. http://jsfiddle.net/teKwN/4/

Please help. Thanks.

Community
  • 1
  • 1
Alex G
  • 3,048
  • 10
  • 39
  • 78

4 Answers4

4

Your html is invalid so the browser tries to figure out what you meant. If you inspect element in chrome, you will see this:

<h4>
    Sometext
</h4>
<h3>
    Another text
</h3>

Because of that, your h4 has no children, so $(this).children('h3') returns no elements.

You will either need change that h3 to something else, or change your structure and the code to reflect the change.

http://jsfiddle.net/teKwN/3/

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • It does have one child object. – Alex G Mar 03 '12 at 05:29
  • Try inspecting it in chrome, it has no child elements unless you count the text node. Chrome interprets it exactly as I have shown above. – James Montagne Mar 03 '12 at 05:29
  • - h3 is a child of h4, same
    span is a child of div?
    – Alex G Mar 03 '12 at 05:33
  • Yes, but as I explained, `

    ` is invalid so the browser tries to figure out the most reasonable VALID way of interpreting it. Chrome comes up with what I posted above, which does not have any child in h4. I checked IE9 and it does the same. Don't nest headings.
    – James Montagne Mar 03 '12 at 05:35
0

Not sure what the exact problem is, but nesting headers probably isn't a good idea. Reorganizing your HTML with the headers as siblings in a container element seems to fix it: http://jsfiddle.net/rjzaworski/sJe3g/

rjz
  • 16,182
  • 3
  • 36
  • 35
0

try

$('h3').toggle('slide');

instead of

$(this).children('h3').toggle('slide');
t q
  • 4,593
  • 8
  • 56
  • 91
0

Check this it works fine

 $('h4').live('click', function() {
        $('h3').toggle('slow');
    });
Sam Arul Raj T
  • 1,752
  • 17
  • 23