tags:

views:

56

answers:

4

Hello, I'm very new in scripting with js. My question is, how can i set an onclick at an h1, which set the display to none at another div?

Here my code:

var iter;

        function toggleGroups()
        {
            function getAllGroups()
            {
                titlebars = document.getElementsByClassName('titlebar_js');
                groupfields = document.getElementsByClassName('inner_groups_gether');
                for(i = 0; i < titlebars.length;i++)
                {
                    tb = titlebars[i];
                    iter = i;
                    tb.onclick = function()
                    {
                        alert(iter);
                        groupfields[iter].style.display = 'none';
                    }
                }
            }           
            getAllGroups();
        }

        window.onload = function()
        {
            toggleGroups(); // Aufruf der Funktion
        }

It doesn't work.

+1  A: 

You really should look at a javascript library such as jquery.

Keltex
-1: This is too vague to be useful. A link to an example of something similar to what was requested that uses jQuery, fine, but this isn't particularly helpful.
David Dorward
+1  A: 

You have trapped into a common JS closure bug. Try this:

tb.onclick = function(val) {
                    return function() {
                    alert(val);
                    groupfields[val].style.display = 'none'; }
                }(iter);

Here you can read more about using closures and event handlers: http://www.webreference.com/programming/javascript/rg36/

alemjerus
+1  A: 
    function toggleGroups()
    {
        var titlebars = document.getElementsByClassName('titlebar_js'),
            groupfields = document.getElementsByClassName('inner_groups_gether'),
            tLength = titlebars.length,
            tb, iter;

        while (tLength--)
        {
            tb = titlebars[tLength];
            iter = tLength;
            tb.onclick = function(iter)
            {
                return function() 
                {
                    alert(iter);
                    groupfields[iter].style.display = 'none';
                }
            }(iter);
        }
    }
    window.onload = function()
    {
        toggleGroups(); // Aufruf der Funktion
    }
Raveren
+2  A: 

Hey, the other guy is right, jQuery is great for solving this very quickly. Example:

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
        jQuery(window).bind("load", function() { 
            $('h1').click(function() {
                $('div.toBeHidden').css('display','none');
            });
        });
    </script>
</head>
<body bgcolor="yellow">
<h1>Click me!</h1>
<div class="toBeHidden">
    This block will be hidden after clicking the h1... 
</div> 
</body>
</html>

Here you are telling the browser to grab any h1 tag and make the divs with class "toBeHidden" to disappear.

You can learn bout jQuery here. It is worth it for sure. I rarely use pure javascript again after having started with jQuery.

Marcos Buarque
Not adequate: the asker wants each `h1` to hide its respective `div`, not all of them.
Raveren