views:

166

answers:

3

Hello! I'm learning JQuery and I wrote a simple AJAX external script for my homepage that tries to load some static markup from a seperate html file and insert it into my homepage when i hover over a link...

$(function()
{
    $('a#contact_link').hover(function() 
    {
        alert('test1'); 
        $('<div id="contact_container">').load('contact.htm #contact', function() 
        {
            alert('test2');
            /*$(this) + '</div>';
            $(this).hide()
                .insertAfter('#header')
                .slideDown(1000)  */  
        });

        return false;
    });
});

at this point, i'm simply trying to get the 'test2' alert box to show, which is why i have the code below that commented out. currently, the 'test1' alert box is the only the one that shows, meaning the 'test2' alert line never gets hit. your thoughts?

here's the snippet of code from my contact.htm file...

<div id="contact_container">
    <div id="contact">
        <p>
        <strong>NAME</strong>: My Name<br/>
        <strong>EMAIL</strong>: My Email<br/>
        <strong>SKYPE</strong>: My Skype Handle<br/>
        <strong>ADDRESS</strong>: My Address<br/>
        </p>     
    </div><!--end contact-->
</div><!--end contact_container-->

thanks so much in advance for your help!

+8  A: 

You were calling $.load using incorrect selector syntax, a selector is not an HTML string, it must conform to the syntax rules set by the jQuery Selectors manual:

$('a#contact_link').hover(function() 
    {
        alert('test1'); 
        $('div#contact_container').load('contact.htm #contact', function() 
        {
            alert('test2');
            /*$(this) + '</div>';
            $(this).hide()
                .insertAfter('#header')
                .slideDown(1000)  */  
        });

        return false;
    });
karim79
thanks so much, though it still doesn't seem to work. any other ideas?
BeachRunnerJoe
i take that back, i think that fixed it, thanks!
BeachRunnerJoe
As a note, it's best to put curly braces on the same line as your function()s. The reasoning is that Javascript performs semicolon insertion. Reference: http://www.youtube.com/watch?v=hQVTIJBZook
Stuart Branham
I've never had problems with putting the curly brace on the next line.
ChaosPandion
@ChaosPandion - you must be very lucky so far! Putting the opening brace on the same line ensures that you never get any problems to do with semi-colon insertion, which can be very tricky bugs to track down. For a small change in style, it is worth it.
Russ Cam
+2  A: 

The selector is probably the cause.

$(function()
{
    $('a#contact_link').hover(function() 
    {
        alert('test1'); 
        $('#contact_container').load('contact.htm', function() 
        {
            alert('test2');
            /*$(this) + '</div>';
            $(this).hide()
                .insertAfter('#header')
                .slideDown(1000)  */  
        });

        return false;
    });
});
ChaosPandion
thanks so much, though that didn't seem to work either. not sure what's going on here...
BeachRunnerJoe
i take that back, it works now. thanks so much!
BeachRunnerJoe
You had me worried there for a bit...
ChaosPandion
+1  A: 

I believe your #contact_container selector is not quite kosher. Check out the selector doco (http://docs.jquery.com/Selectors) at the JQuery site. I think something like this might be a little better:

$(function()
{
$('a#contact_link').hover(function() 
{
    alert('test1'); 
    $('#contact_container').load('contact.htm #contact', function() 
    {
        alert('test2');
        /*$(this) + '</div>';
        $(this).hide()
            .insertAfter('#header')
            .slideDown(1000)  */  
    });

    return false;
});
});
Sven Schott