proseidon
proseidon

Reputation: 2305

Unable to find an element inside of an element?

I have a strange error that I'm not sure what is not working.

I've got this html:

    <div id="active">
        <label for="slide1"></label>
        <label for="slide2"></label>
        <label for="slide3"></label>
        <label for="slide4"></label>
        <label for="slide5"></label>
    </div> <!-- #active -->

and this jquery:

$("#active label").click(function () {
    alert('hi');
});

Yet whenever I click on the element, the jquery does not execute. I tested in chrome to make sure I was clicking on that element, and I am. Is there a problem with the code pasted here or is my error being caused by something else?

Upvotes: 2

Views: 68

Answers (3)

Codegiant
Codegiant

Reputation: 2150

Try this

$(document).ready(function () {
    $("#active label").on('click', function () {
        alert('hi');
    });
});

Upvotes: 0

Matthew Scragg
Matthew Scragg

Reputation: 4638

$(function(){
  $("#active label").click(function () {
      alert('hi');
  });
});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

Your code should work, assuming you have included it inside a document ready handler, and included jquery.js

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<!-- ^ Google CDN used as an example, a local file will work too -->
<script type="text/javascript">
    $(function() {
        $("#active label").click(function () {
            alert('hi');
        });
    });
</script>

Upvotes: 2

Related Questions