Getting the 2nd child element

Written by leif. Posted in Javascript, Programmering, Tips

A number of ways to do this one. I’m going to assume the toplevel div has an id of ‘top’. This is probably the best one:


$('#top > :nth-child(2)').whatever();

or


$('#top').children(':first-child').next().whatever();

or if you know for a fact there are at least 2 children


$($('#top').children()[1]).whatever();

JQuery slideToggle flere elementer

Written by r2d2. Posted in Javascript

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
  $(document).ready(function(){
    $(".containerdiv").click(function(){
      $(this).find(".inch").slideToggle("slow");
    });
  });
</script>
</head>
<body>

<h2>This is a heading</h2>
<div class="containerdiv">klik her
<div class="inch">dette er teksten.</div>
</div>
<br><br>
<div class="containerdiv">klik her2
<div class="inch">dette er teksten2.</div>
</div>

</body>
</html>