2

I have this code here that should send off an alert...

jQuery(".wpcart_gallery .thickbox").click(function($){
            alert('hi');
        });

But it doesnt work :( there are no errors and the alert never appears. I have the correct JQuery.js file in my header...is there something wrong with my syntax?

Here is the HTML code

<div class="wpcart_gallery" style="text-align:center; padding-top:5px;">
<a class="thickbox cboxElement" href="DSC_0037.jpg" rev="DSC_0037.jpg"></a>
<a class="thickbox cboxElement" href="DSC_0036.jpg"  rev="DSC_0036.jpg"></a>
<a class="thickbox cboxElement" href="DSC_0118.jpg" rev="DSC_0118.jpg"></a>
</div>

I am trying to have a click event when one of the links inside wpcart_gallery is clicked.

Here is the page, blow the big image there are three images, I am looking for click event to trigger if any of those images are clicked...http://www.taranmarlowjewelry.com/?wpsc-product=product-1-2

4
  • 3
    Is that JavaScript within a $(document).ready(function() { ... }); call? Can you provide the site or a jsFiddle in which this is observable? Commented Sep 17, 2012 at 20:32
  • Yes it is inside a jQuery('document').ready(function($){....}); Commented Sep 17, 2012 at 20:34
  • 'document' and document are different. Remove the 's and try it. Commented Sep 17, 2012 at 20:35
  • that wont work at all inside wordpress Commented Sep 17, 2012 at 20:44

3 Answers 3

3

I fixed the issue by

jQuery(".wpcart_gallery img").click(function($){
            alert('hi');
        });
Sign up to request clarification or add additional context in comments.

Comments

2

Could be that you have invalid HTML. None of the following have closing tags:

<a class="thickbox cboxElement" href="DSC_0037.jpg" rev="DSC_0037.jpg">
<a class="thickbox cboxElement" href="DSC_0036.jpg"  rev="DSC_0036.jpg">
<a class="thickbox cboxElement" href="DSC_0118.jpg" rev="DSC_0118.jpg">

If I were jQuery I'd be confused.

If I add closing tags it works fine: http://jsfiddle.net/GtgEy/

1 Comment

Yes they have those, just forgot to put them in
0

Likely you need to use delegated bindings:

jQuery(window).on('click', ".wpcart_gallery .thickbox", function(ev){
    alert('hi');

    ev.preventDefault();
});

You can replace window with some other parent element if you can. Given:

<div id="test">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
    <a href="#">Link 4</a>
</div>

you could do:

jQuery("#test").on('click', 'a', function(ev) {
    alert('hi');

    ev.preventDefault();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.