Peter Boomsma
Peter Boomsma

Reputation: 9808

Can't find the right selector

I'm trying to have a class call a function when it's clicked but it's not working.

$('.container_vragen').click(appendContainerVragenToContent_vragen);

with this code the function is called, but I want to call the action on .container_vragen only when it's in the div #geregeld

$('.container_vragen' , '#geregeld').click(appendContainerVragenToContent_vragen);

And when I define that .container_vragen is inside #geregeld it does not call the function appendContainerVragenToContent_vragen

What's the right selector for this?

Upvotes: 0

Views: 79

Answers (2)

Adil
Adil

Reputation: 148110

You are passing string instead of DOM object in the context, find more about selector and context here.

Change to

$('.container_vragen' , $('#geregeld')).click(appendContainerVragenToContent_vragen);

or using descendant selector

 $('#geregeld .container_vragen').click(appendContainerVragenToContent_vragen);

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382122

The simple solution is to use the power of selectors :

$('#geregeld .container_vragen').click(appendContainerVragenToContent_vragen);

But note that this won't work if the DOM changes after you call. If you want to allow this, use

$(document.body).on('click', '#geregeld .container_vragen', appendContainerVragenToContent_vragen);

Upvotes: 1

Related Questions