tags:

views:

96

answers:

3

I have read that it is better performance to have jquery use a DIV's ID instead of a CLASS This code below uses a div class instead of ID, can you make it use the ID instead?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;  
<script type="text/javascript" >
 $(document).ready(function(){
  setTimeout(function(){
  $(".flash").fadeOut("slow", function () {
  $(".flash").remove();
      }); }, 2000);
 });
</script>
<div class="flash" id="flash">tessssssssssssssssssssssssssssssssssst</div>
+2  A: 

Use a hash (#) instead of a dot (.) :

$("#flash").fadeOut("slow", function () {
$("#flash").remove();
...
RichieHindle
Cool thanks
jasondavis
+8  A: 
$('#flash').fadeOut...

The docs are good at explaining this.

It is just like in CSS:

'#' for id.

'.' for class

redsquare
good to know, I didn't know that about css either, thanks
jasondavis
+1  A: 

You can also use $(function(){}); as a shorcut to $(document).ready(function(){}); Yes you can use (#) insted of (.). Also you need not require to write $("#flash") again inside below code snippet

    $("#flash").fadeOut("slow", function()
    {
        $("#flash").remove();
    });

you can write

    $("#flash").fadeOut("slow", function()
    {
        $(this).remove();
    });

ie you can do with this keyword

Try this example:

<html>
<head>
    <title></title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;  
<script type="text/javascript" >
    $(function()
    {
        setTimeout(function()
        {
            $("#flash").fadeOut("slow", function()
            {
                $(this).remove();
            });
        }, 2000);
    });
</script>


</head>
<body>
<div class="flash" id="flash">tessssssssssssssssssssssssssssssssssst</div>

</body>

</html>
Raghav Khunger