tags:

views:

134

answers:

6

I am trying to do something similar to the alert box that we get here on SO when we get new comments and answers, the orange box that sticks to the top of the screen, I have something similar but I am trying to get an X close link on it, to close it on click

Here is what I have but it is giving me errors saying the close_error_click() is not defined?

<script type="text/javascript" >
function close_error_click(){
    $("#notify-container").fadeOut("slow", function () {
    $("#notify-container").remove();
}
</script>

<div id="notify-container">
some other code here
<a onclick="close_error_click" title="dismiss this notification">×</a>
</div>
+1  A: 

Yes just do:

close_error_click(); in your onclick.

You missing the brackets on the end.

Kelsey
I tried this, when I click the link though I get this error; close_error_click is not defined
jasondavis
+1  A: 

Try

<a onclick="close_error_click();" title="dismiss this notification">×</a>
Rob
+4  A: 

Rather than using an onlick attribute, you can add the following line after your function:

$("#notify-container a").click(close_error_click);

This technique known as Unobtrusive JavaScript keeps your markup clean from Javascript. Here's a complete example:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        $(function()
        {
            $("#notify-container a.close").click(function()
            {
                $("#notify-container").fadeOut("slow", function()
                {
                    $(this).remove();
                });
            });
        });
        </script>
    </head>
    <body>
        <div id="notify-container">
            some other code here
            <a href="#" class="close" title="dismiss this notification">×</a>
        </div>
    </body>
</html>
Jason Weathered
make sure the a has a unique ID or class, otherwise clicking any link in the container will close it.
Moak
Moak: Good point. I have added an example which selects the anchor via a class.
Jason Weathered
That works great thanks, 1 thing maybe you know how to fix? I requires a double click to close, could it be change to close on 1st click somehow?
jasondavis
jasondavis: I tested in both Safari 4.0 and Firefox 3.5 and was unable to reproduce this issue. What browser are you using?
Jason Weathered
nevermind seems to be working correctly now, thanks
jasondavis
A: 

Also, you might not want the code in the markup:

<script type="text/javascript" >
function close_error_click(){
    $("#notify-container").fadeOut("slow", function () {
    $("#notify-container").remove();
    return false;
});
}
$(document).ready(function(){
$('#notify-container .closewindow").click(close_error_click);
});
</script>

<div id="notify-container">
some other code here
<a href="#" title="dismiss this notification" class="closewindow">×</a>
</div>
Moak
there is some sytax error, missing something from this line $('#notify-container .closewindow").click(close_error_click);
jasondavis
A: 

you need to add () to the name of the function ×

or in jquery

$(document).ready(function() {

$("#id_of_the_a_element").click(close_error_click);

});

function close_error_click(){
$("#notify-container").fadeOut("slow", function () { $("#notify-container").remove();}

A: 
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <div id="notify-container">
        some other code here <a id="CloseLink" href="#" title="dismiss this notification">x</a>
    </div>
</body>
<script type="text/javascript">
    $(function()
    {
        $('div#notify-container a#CloseLink').click(function(e)
        {
            $('div#notify-container').fadeOut('slow', function()
            {
                $(this).remove();
            });
            e.preventDefault();
        });
    });
</script>
</html>
Raghav Khunger