79

When you click you a checkbox I want the message to fade in slowly.

Why doesn't .fadeIn() work in this example?

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
        <title>Text XHTML Page</title>
        <link href="css/main.css" rel="stylesheet" type="text/css" media="all"/>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript" src="javascript/main.js"></script>       
    </head>
<body>
    <div class="checkboxList">
        <div class="title">Languages:</div>
        <div class="row"><input class="checkbox" type="checkbox"/><span class="label">Ruby</span></div>
        <div class="row"><input type="checkbox"/><span class="label">PHP</span></div>
        <div class="row"><input type="checkbox"/><span class="label">C#</span></div>
        <div class="row"><input type="checkbox"/><span class="label">Python</span></div>
        <div class="row"><input type="checkbox"/><span class="label">JavaScript</span></div>
    </div>
    <p id="message"></p>
</body>
</html>

javascript:

google.load("jquery", "1.3.2");

//run when page is loaded
google.setOnLoadCallback(function() {

    $('.checkboxList .row').css('color','red');
    $('.checkboxList input').attr('checked', true);
    $('.checkboxList input').bind('click', function() {
        $('#message').html("You clicked on a checkbox.").fadeIn('slow');
    });

});
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

3 Answers3

233

No fadeIn is done because the #message element is visible, hide it, add the content and fade it in:

$('#message').hide().html("You clicked on a checkbox.").fadeIn('slow');
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 7
    A Content Management System in the form of a human being - awesome! – JoseBazBaz Dec 31 '12 at 01:10
  • What if I add several `li` elements after 1sec one by one and want to show with your solution? How to just fadein the li I added not all li's? Like `$("#oFnav").append("
  • Business Process Study
  • ").hide().fadeIn(fadeIndur);`. It hides and fadein all lis not just addeed. – RN Kushwaha Nov 10 '14 at 08:03
  • Modified: `$('#message').fadeOut('slow').html("You clicked on a checkbox.").fadeIn('slow');` will let the faded in html blink 2 times... funny bug :P – Kerim Yagmurcu Mar 16 '19 at 19:12