3

I want to imitate an anchor click when a user clicks on the containing <TD> but having problems.

This is the JS part:

$('contactTab').click(function() { $('contactTabLink').trigger("click"); }); 

And this is the HTML part:

<td class="previewTabs" id="contactTab"> <a class="previewTabLink" id="contactTabLink" rel="#contactOverlay">CONTACT</a> </td> 

When somebody clicks the <TD> contactTab, it should trigger a click event on <A> contactTabLink which then launches an Overlay. My problem is that the JS function with click listener is NOT firing at all.

Can anybody see where I am going wrong?

EDIT 1:

I have changed the selectors to have #'s but it still won't fire the function.

3
  • $('contactTab') will try to find an element with tag contactTab. You want the ID-selector. Triggering the click event won't make the browser follow the link though. Commented Jan 4, 2012 at 16:11
  • Yeah, you're missing the "#" in front of the IDs in your jQuery selectors. Use $('#contactTab'). and not $('contactTab') Commented Jan 4, 2012 at 16:11
  • where's the click listener defined for $('contactTabLink')? You have the click being triggered, but I don't see any listener defined for it, so there's no action to take on a click event. Commented Jan 4, 2012 at 16:12

5 Answers 5

15

Your selector is not correct.

It should be this:

$('#contactTab').click(function() { $('#contactTabLink').trigger('click'); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Your second line is still missing the #
1

Your selectors are wrong.
You need # for ID selectors.

Comments

1

Your selectors are wrong - you're using id's so you should have a '#' in front.

$('#contactTab').click(function() { $('#contactTabLink').trigger("click"); }); 

Comments

1

I think something to notice here, with the way your html is formatted, $('#contactTabLink').trigger("click") will cause $('#contactTab').click to fire since it is a parent of #contactTabLink, thus causing an infinite loop of recursion. Some browsers will print an error to the console or simply not respond. You could arrange your html accordingly to avoid this:

<td class="previewTabs" id="contactTab"> <a href="javascript:;" class="previewTabLink">CONTACT</a> </td> <a class="previewTabLink" id="contactTabLink" rel="#contactOverlay"></a> 

Comments

0

After adding hashes (#) to the selector, make sure that function is wrapped in $(document).ready(function() { yourFunction })...

$(document).ready(function() { $('#contactTab').click(function() { $("#contactTabLink").trigger("click"); }); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.