7

I'm trying to make a div fadeIn when another div is clicked and fadeOut again when another div is clicked (which would be the close button) but my code doesn't work, did I forget something?

Here's the CSS:

body{
    margin: 0;
    padding: 0;
    text-align: center;
    background-color:#f0f2df;
}

#container{
    border: solid 1px #f0f2df;
    background-color:#f0f2df;
    text-align: left;
    margin: auto;
    width: 939px;
    height: 570px;
    top:41px;
    position:relative;
}
#contact_form{
    display: none;
    background-image:url(../images/bg.png);
    width: 703px;
    height: 379px;
    position:absolute;
    left:236px;
    bottom:34px;

}
.contact_close{
    display:none;
    background-image:url(../images/close.png);
    width:17px;
    height:17px;
    position:absolute;
    right:5px;
    top:135px;
}

The HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
<title>test</title>
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript' src='js/click.js'></script>
</head>

<body>
    <div id="container">
        <div class="button_contact"></div>
        <div id="contact_form">
        <div class="button_close"></div></div>
</div>
</body>
</html>

and the JavaScript

$(document).ready(function(){ 

 $("button_contact").click(function() { 
    $("#contact_form").fadeIn("slow");
});

 $(".contact_close").click(function() { 
      $("#contact_form").fadeOut("slow"); 
    });
 });
Bruno
  • 155
  • 1
  • 3
  • 9

7 Answers7

11

you need the "." before button_contact

$(document).ready(function(){ 
  $(".button_contact").click(function() { 
    $("#contact_form").fadeIn("slow");
  });

  $(".contact_close").click(function() { 
    $("#contact_form").fadeOut("slow"); 
  });
});
scunliffe
  • 62,582
  • 25
  • 126
  • 161
9

jQuery also has a .toggle() function that allows you to pass multi-functions that are toggled between each other when the element/s are clicked.

http://api.jquery.com/toggle/ a nice function because you can add as many functions as you want.

$(document).ready(function(){ 
    $(".button_contact").toggle(function() { 
        $("#contact_form").fadeIn("slow");
    },
    function() { 
        $("#contact_form").fadeOut("slow"); 
    });
});
Alex
  • 1,457
  • 1
  • 13
  • 26
Mike
  • 2,547
  • 1
  • 24
  • 36
2

You forgot a . before the button close:

$(".button_contact")....

Should work.

Alex
  • 1,457
  • 1
  • 13
  • 26
akearney
  • 36
  • 2
1

The selector on your fadeIn button is a bit off. Your original code matches an element with a node name of button_contact not with a class of button_contact.

Try:

$(".button_contact").click(function() { 
    $("#contact_form").fadeIn("slow");
});
Jon Cram
  • 16,609
  • 24
  • 76
  • 107
1

$("button_contact").click(function() { should be

$(".button_contact").click(function() {
rosscj2533
  • 9,195
  • 7
  • 39
  • 56
1

Try this:

$(document).ready(function(){ 

 $(".button_contact").click(function() { 
     $("#contact_form").fadeIn("slow");
 });

 $(".button_close").click(function() { 
  $("#contact_form").fadeOut("slow"); 
  });
 });
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
0

I'm sorry but will this syntax work if the link being clicked is an ajax link corresponding to another DIV? I can't seem to get it to work!

<script type="text/javascript" src="js/jquery-ajaxLink.js"></script>
    <script type="text/javascript">
        $(document).ready(function() { 
        $(".ajaxLink").ajaxLink();
        $(".ajaxlink").click(function() { 
     $("#content").fadeIn("slow");
  });
 });
    </script>


            <ul id="mainNav">
    <li> <a class="ajaxLink" href="test1.htm">Who We Are </a></li>
    <li> <a class="ajaxLink" href="test2.htm">Benefits</a></li>
    <li> <a class="ajaxLink" href="test2.htm">Commercial Terms</a></li>
    <li> <a class="ajaxLink" href="test3.htm">Property Types</a></li>
    <li> <a class="ajaxLink" href="test3.htm">Commercial Info</a></li>
            </ul>

<div id="content"></div>
Jeff Voss
  • 3,637
  • 8
  • 46
  • 71