views:

76

answers:

4

this code:

$(document).ready(function() {
$('body').click(function(evt) {
if(evt.target.nodeName === 'A' && $(evt.target).hasClass('cross-link')) {
$('a[href=#2]').trigger('click');
} });});

given me and error of "too much recursion"

one might think that I should just attach a handler to the crosslink element. i tried this, but I couldn't get it to work because the DOM loads before the cross-link class elements are created. what do I need to do to fix this or do you have a better idea of what I should do to implement what I'm trying to do?

if you want to see the error for yourself, do to eataustineat.com/testfolder/ type in a 'd' in the search field select dog almighty (this is where you should notice the "too much recursion error" it will move the div to the left, but it will do so very buggily.

+2  A: 

You can use live or delegate to add listeners for elements that are created later:

$("a.cross-link").live("click", function()
{
   $('a[href=#2]').trigger('click');
   window.location.hash = "#2";
});

However, click does not trigger the default event of going to the URL, so you need to do that manually.

Matthew Flaschen
this is the best way. I would have done a project this way but only found out after went through the process of creating the element and attaching an on click function to the object after it was created.
Kieran
what do you mean i need to manually do the default event? is there a tutorial you would recommend that would explain this?
scifirocket
@scifirocket, calling `trigger('click')` will not actually go to the URL "#2", so you need to do so manually. I do that in the `window.location.hash` line.
Matthew Flaschen
I'm still getting the "too much recursion" error. You can see it at: eataustineat.com/testfolder/index.php
scifirocket
@sci, yes, the issue was explained by sunn0. You have a a element with both `cross-link` class and `href` `#2`. That means when you trigger `click`, you're also calling the event handler you're currently in. Hence, the infinite recursion.
Matthew Flaschen
Use this solution instead of mine but remove $('a[href=#2]').trigger('click'); as the only reason you are calling .click() seems to be to navigate to "#2"
sunn0
this doesn't work. it doesn't seem to activate the javascript associated with the cross-link class. simply forcing the location to #2 wont move the slider.
scifirocket
i ended up using a different slider all together. but the .live function was what i ended up using for the new solution. if you care, you can see what i was trying to do at www.eataustineat.com/testfolder2/
scifirocket
A: 

Well the recursion comes from triggering

$('a[href=#2]').trigger('click');

When this element is clicked from the event it throw yet another event which will be handled by the same code and so on.

This should work:

    $(document.ready)(function(){
      $('.cross-link').click(function(){
       #('a[href=#2').click();
});
});

Also performance-wise it is more optimal to add an id to your second link because selecting by an id is faster than selecting by an attribute. If you still want to go with selecting by href and there is only one such link do:

#('a[href=#2 :first').click();
sTodorov
`hasClass` is a jQuery function, not his class. And this won't work because the elements don't exist yet, as noted in the question.
Matthew Flaschen
+1  A: 

If elements that need an existing event are added after document creation you can use live

$(document).ready(function() {
    $('.cross-link').live(function() {
        $('a[href=#2']).click(); //No cross-link class allowed on this element as it is responsible for the recursion
    });
});
sunn0
firebug tells me: "f is undefined"
scifirocket
i'm going to try using this method. thanks!
scifirocket
I implemented this, but I continued to get the recursion error. code looks like this now: $(document).ready(function() { $('.cross-link').live('click', function() { $('a[href=#2]').click(); });});
scifirocket
Does your <a href="#2"> have class=".cross-link" as well?
sunn0
I want everything with class 'cross-link' to have href=#2. I'm not sure this answers your question.
scifirocket
Can we see some HTML as well please? Otherwise just go for the above solution. Makes more sense. You can even skip the $('a[href=#2]').trigger('click'); if the only reason for triggering click is to go trigger the default "Navigate to URL" event.
sunn0
I'm still getting the "too much recursion" error. You can see it at: http://eataustineat.com/testfolder/index.php
scifirocket
A: 

for referance to my comment above this is how i did it. I recommend using live tho...


var open = function (myObj, animationTime) {
    //do stuff
    $(myObj).unbind('click');
    $(myObj).click(function () {
        close(myObj, timer);
    });
}

var close = function (myObj, animationTime) {
    //dostuff
    //remove close click event and then rebind the click event to open
    $(myObj).unbind('click');
    $(myObj).click(function () {
        open(myObj, timer);
    });
}

$(".mySelector").click(function () {
    open($(this), timer);
});


Kieran
where open would add the element to the dom and remove / add a function and close would removed an element from the dom and add / remove the onclick funtion...
Kieran