1

I followed this thread. I now have:

<a href="#" onclick="$('#gc').load('test');">reload</a>... </span>
<div id="gc">
    empty
</div>

This is what I am getting:

Uncaught exception: TypeError: Cannot convert '$('#gc')' to object
Error thrown at line 1, column 0 in <anonymous function>(event):
    $('#gc').load('test');

What is that? I thought I would be able to select a div and replace the contents with load()?

1 Answer 1

4

Try attaching the event handler on domready:

HTML:

<a href="some-url" class="reload_link">reload</a>
...

JS:

(function($) {
  $(function() {
     $('.reload_link').click(function() {
        $(this).next('div').load(this.href);
        return false;
     });
  });
})(jQuery);

I like to wrap my jQuery code inside of an anonymous function using a $ argument to avoid name conflicts with other libraries. (Of course, I could just use jQuery.noConflict() as well)

The way this works is it retrieves all elements in the document that have a class of reload_link, then for each one, it retrieves the next <div> element and invokes load, passing in the current <a> element's href attribute.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that worked. Can I easily mark more <a>-elements with reload_link? And can you shortly explain WHY this works?
@user469110 You can use a class and select on those, maybe even utilize the href attribute to determine which URL gets loaded...
Why it works? Because Jquery gets ready when the page is fully loaded. Because "onclicks" get registered before jquery is ready, it cant work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.