views:

158

answers:

4

Hi Everyone,

I'm running into a problem with jquery live event binding on a link. When the link is added to the page it works fine, however when another link is added to the unordered list it requires two clicks for the click event to fire on either link. Any ideas?

$("div#website-messages ul li a").live("click", function() {
    var link = $(this);
    changeTab(link.attr("href"));
    $(link.attr("title")).focus();
    return false;
});

EDIT: OK I've narrowed down the problem. If I take out return false, the event fires every time. The problem is then the page jumps to the top. Any ideas to stop that?

Code that creates the links:

Validation.validate = function() {
    var html = "";
    for (var i = 0; i < errors.length; i++) {
        html += "<li><a href='" + errors[i].tabid + "' title='" + errors[i].elementID + "'>" + errors[i].message + "</a></li>";
    }
    $("div#website-messages ul").html(html);
}

ChangeTab Function

function changeTab(changeTo) {
    changeTo = changeTo.substr(changeTo.indexOf("#"), changeTo.length);
    $("#tabs div").hide();
    $(changeTo).show();

    $("ul.navigation li a").removeClass("selected");
    $("ul.navigation li a[href='" + changeTo + "']").addClass("selected");
}

SOLVED I had a blur event on the text inputs that were being focused to that validated them. If I clicked on one of the errors and focused to the first textbox, then clicked on the second error, it would focus to the second textbox but fire the blur event and not focus. Thank you all SO much for your help and suggestions, this was driving me crazy all day.

+1  A: 

EDIT: OK I've narrowed down the problem. If I take out return false, the event fires every time. The problem is then the page jumps to the top. Any ideas to stop that?


try

event.preventDefault();
Jasper De Bruijn
That brings back the same problem I had before. Any other suggestions?
Jon
+1  A: 

Since it jumps to the top ... what is the content of the actual href? If it is something like "#", then it will likely jump to the top. If the result of the click is to change the content on the page, instead of actually navigating away from the page, you might consider using:

<a href="javascript:void(0)">linky</a>
Matt
This stops the page from jumping to the top but also stops the event from firing every time.
Jon
I guess I'm confused as to what you are trying to accomplish - does the link navigate to a new page or not? If you're just using the link to trigger events on the page, then you have to return false or use javascript:void(0), or # as the href. If you don't return false, it will navigate to the url of the href.
Matt
+1  A: 

this prevent link jumps to the top:

<div id="website-messages">
<ul>
<li><a href="javascript:;" >my link</a></li>
</ul>
</div>

this make js it fire every time!

$("div#website-messages ul li a").live("click", function(e) {
    //this make it fire every time!
    e.preventDefault();
    var link = $(this);
    changeTab(link.attr("href"));
    $(link.attr("title")).focus();
});

UPDATE:

your link should be something like this:

<li><a id='" + errors[i].elementID + "' href='javascript:;' title='" + errors[i].tabid + "'>" + errors[i].message + "</a></li>
aSeptik
Same problem :-( . Any other suggestions?
Jon
I have a form that has a tabbed interface. On the right hand side I list all of the errors for the form and each error is clickable. When the user clicks the error, it changes to the correct tab and should focus to the text input associated with the error. Do you know of another way to accomplish what I am trying to do?
Jon
maybe the problem is elsewhere, the correct form of prevent link jump on top is or using return false; or preventDefault; can you add some other code!?
aSeptik
I'll add the code that creates the links.
Jon
Nevermind my last comment on focus(); my bad; check instead the changeTab(link.attr("href")); is probably the tabs that causing the mistake! make a try!
aSeptik
Still not working, I added the changeTab function to the code above. Thanks for all of your help!
Jon
OK, I've narrowed it down even further. If I am on any other tab and click any of the error links it works perfectly. The only time it does not work is when I am in the same tab as the as the tab the error link is taking me to.
Jon
A: 

I had a blur event on the text inputs that were being focused to that validated them. If I clicked on one of the errors and focused to the first textbox, then clicked on the second error, it would focus to the second textbox but fire the blur event and not focus.

Jon