0

I have a javascript snippet, which intercepts link clicks and turns them to hashes (I know that hash-id isn't in the W3C Recommendations :) )

$("a").click(function(event) {
    event.preventDefault();
    newHash = $(this).attr("hash-id");
    console.log(newHash);
    if (typeof newHash === "undefined"){
    window.location = $(this).attr("href");
    }
    else{
    window.location.hash = "#" + newHash;
    }
});

Basically whenever a user clicks any link, it's supposed to get it's "hash-id" if there is none, than it just loads the page regularly. Now: it works great on my site, footer, header, but after I click an "injected" content

$("#container").load(href + " #content");

It doesn't work, doesn't return any output to console, alert, nada.

I tried putting the code at the begining, end, footer, in both pages, but nothing works, anyone got any clue how to make it work?

2
  • Have you already tried $("a").live("click", function(){..})? Commented Oct 4, 2011 at 10:27
  • paste it as an answer so I'll accept, I haven't known this function Commented Oct 4, 2011 at 10:30

2 Answers 2

3

You should use $("a").live("click", function(){..}), so that the event listener is binded to all (now and later) a elements.

When you use .click(func) =.bind("click", func), the event listener is only added for all current (existing) elements, not to the elements which are added in the future.

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

Comments

1

Your should use live() instead of click(), which implies bind().

bind only works on current elements selected by the jQuery object, while live also affects new elements added to the document.

Check the docs. http://api.jquery.com/live .

Comments

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.