0

I have some links that have events bound to them and some links that do not.

These links are having their click event triggered by a change in a select box.

See this fiddle

Is there a way for jquery to visit a link if there is no event bound that link?

I do not want to reference the url within the select box vlaue attribute, I want to reference the url in the anchor tag.

something like (pseudo code) -

 if (link.hasEventHandler('click')){
    trigger click
 } else {
    window.location = link.attr('href')
 }

EDIT - There's a plugin that does exactley what I need to do here - https://github.com/sebastien-p/jquery.hasEventListener

File size is an issue, so I'd like to avoid using it.

I'm using the latest jquery so cannot use .data('events')

Finnnn
  • 3,530
  • 6
  • 46
  • 69

3 Answers3

0

You could implement something the Dropdown button from Twitter Bootstrap (http://twitter.github.com/bootstrap/components.html#buttonDropdowns). Bootstrap also allows you to customize your download. When you select only dropdown, it comes out to ~40kb. It doesn't even require jQuery because it's all CSS.

Joshua Vega
  • 598
  • 1
  • 8
  • 22
0

Looking at your code, you have to select the link with the href attribute which is same with the selected value in your combobox. Something like:

$('#nav').on('change', function(event) {
      var targetHref = $(this).find("option:selected").val();
      $('a[href="'+targetHref+'"]').each(function() {
           $(this).trigger('click');
      });
});

Assuming you have a click event for your links. If not, just give your links the same class and write a click event for that class to set the window location to the href attribute of the object.

Pabuc
  • 5,528
  • 7
  • 37
  • 52
0

As far as I am aware you can't 'fake click' in JS. The click event trigger simply triggers the onClick JS event, and does not simulate clicking on a link in the browser, entirely. However, all you need to do is grab the href and redirect to that page using JS. I updated your fiddle with a couple of lines that do this.

http://jsfiddle.net/9j8QS/1/

$('a:not(.event-bound)').click(function(event) {
    window.location.href = $(this).attr('href');
});

The selector selects all anchor tags that don't have the class event_bound.

Mike Campbell
  • 7,921
  • 2
  • 38
  • 51