-1

I've run into an issue where click events are being ignored on elements selected by their attribute values in webkit browsers.

To clarify, I'm attempting to trigger a click on the selected event, not bind a function to the click event.

The HTML

<div class="my_list">
    <a href="#thumb_1">Thumb O1</a>
    <a href="#thumb_2">Thumb O2</a>
    <a href="#thumb_3">Thumb O3</a>
</div>

The jQuery

var my_var = 1;
$('.my_list a[href="#thumb_' + my_var + '"]').click();

The event fires in Firefox, but not Safari or Chrome.

Also Tried

var my_var = 1;
$('.my_list a[href="#thumb_' + my_var + '"]').trigger('click');

Any help would be greatly appreciated, thanks.

EDIT

To whomever voted to close this as 'not a real question' ... please clarify otherwise I'll assume that you're a zealot who's life is so empty all you have to keep you going is this BS of voting to close questions on SO.

I assure you this is a real question. It's a real problem I'm currently trying to solve so that my real client will be happy and I can get paid real money now that I've completed my deliverables.

AJB
  • 6,495
  • 11
  • 53
  • 80
  • Have you tried to rename your id's? Remove the underscore if possible and test again. Normally jquery can handle this but I remember me debugging 4 hours after I found out that `_n` is a bad idea! – Tim Vermaelen May 02 '13 at 20:50
  • Hi @TimVermaelen, the underscore is actually only in the example code above. The actual code I'm working with doesn't have any underscores. And to be honest I'm not sure why they would make a difference. Does jQuery have issues with underscores that I'm not aware of? – AJB May 02 '13 at 20:52
  • As said in previous comment, normally not an issue. I'm talking about over 5 years back. Can you setup a fiddle to reproduce the problem? – Tim Vermaelen May 02 '13 at 20:54
  • As seen in this fiddle the javascript isn't the problem (works fine in chrome). http://jsfiddle.net/jKNXF/1/ Are you expecting the hash tag to show up? – Justin Bicknell May 02 '13 at 20:56
  • Hi @JustinBicknell, I'm actually atttempting to trigger a click, not bind a function to the click event. I'm editing the original question to clarify. – AJB May 02 '13 at 21:02
  • @AJB that fiddle is triggering the click though, that why you see the alert, because the js triggered the click event succesfully – Justin Bicknell May 02 '13 at 21:03
  • Ah, right. Sorry ... okay, so the above Javascript is fine ... it just doesn't work in my app for some other reason. – AJB May 02 '13 at 21:07
  • and just to be sure ... you do wrap a DOM ready statement around your javascript, right? Because chrome loads pretty quick ... ^^ – Tim Vermaelen May 02 '13 at 21:07
  • Yes, the above code is running within $(document).ready(function(){ ... }); – AJB May 02 '13 at 21:09

2 Answers2

1

Seems working fine in Chrome: jsfiddle

var my_var = 1;
$('.my_list a[href="#thumb_' + my_var + '"]').click(function(){
    alert("you click thumb"+ my_var);
});

Since you define my_var equals to 1, only the first "Thumb" will be attached the click handler.

zhujy_8833
  • 531
  • 4
  • 6
  • Hi @zhujy, actually I'm attempting to trigger a click on the element, not bind a function to the click event. I'll edit the original question to clarify. – AJB May 02 '13 at 21:01
  • @AJB Sorry, I misunderstood. I modified the jsfiddle, and it seems still work for me. Could you check it out? http://jsfiddle.net/2gFCj/1/ – zhujy_8833 May 02 '13 at 21:08
  • No worries @zhujy ... You're right, the code (above, and in your fiddle) works just fine. It just won't work in my app so I've got to find out what other reason is preventing it from firing. Thanks for your help. – AJB May 02 '13 at 21:10
1

http://jsfiddle.net/jKNXF/1/ As you can see the javascript works as expected. If you see the alert come up, that means that the javascript was able to trigger the click event on that a tag.

Are you expecting the hash tag to show up in the url? If so set location.hash to whatever you need to. If you are trying to scroll to an element, you can use the scrollTop method, see jQuery scroll to element

Community
  • 1
  • 1
Justin Bicknell
  • 4,716
  • 16
  • 25