Jorge
Jorge

Reputation: 18237

Can't bind a click function

I found this component made it with jquery, My problem it's that want to bind the click in the elements of the components which are a a element inside the div called menu to get the ID when are clicked. It doesn't work and I don't understand why. Could anybody see the problem??

Here's my js

    $(document).ready(initialize);

    function initialize() {

    $('#hierarchybreadcrumb').menu({
        content: $('#menu').html(),
        backLink: false
    });

    $('#menu a').bind('click',obtenerId);   
}

    function obtenerId(){

        alert($(this).attr('id'));
    } 

And here's my live demo

Upvotes: 1

Views: 1125

Answers (4)

brenjt
brenjt

Reputation: 16297

Looking further in your code, none of the <a> tags in the #menu have ids so it alerts undefined. Try giving them ids. That's where I would start.

Upvotes: 1

Andres
Andres

Reputation: 2023

I modified it a bit and got the result I think you want.

$(document).ready(function () {
   $('#menu a').click(obtenerId);   
});

function obtenerId(){
    $("#menuSelection").html($(this).attr('id'));
}

<div style="display:table-row">
    <ul id="menu">
        <li><a href="#" id="1">opcion 1</a></li>
        <li><a href="#" id="2">opcion 2</a></li>
    </ul>
</div>
<div style="display:table-row">
    <p id="menuLog">Elegiste: <span id="menuSelection"></span></p>
 </div>

Upvotes: 0

Naftali
Naftali

Reputation: 146302

Aaaaaand then you look at the console (for your live demo)

FAIL!

Upvotes: 2

David Nguyen
David Nguyen

Reputation: 8528

$('a').bind('click',obtenerId);

Mind you I think you want to bind it to the actual specific menu button...

$('#hierarchybreadcrumb').bind('click', function)

Upvotes: 0

Related Questions