2

I just want to disable the ability for a user to click, with the exception of links

$('selector').children().not('a').click(function(e) {
  return false;
});

this doesn't work.. thanks

3
  • 2
    Can you show your markup? Commented Aug 23, 2013 at 14:14
  • What does "doesn't work" mean? Are you getting errors? Is it partially working? Commented Aug 23, 2013 at 14:14
  • Disable the ability for users to click what? Buttons? And you're selecting elements called selector. Commented Aug 23, 2013 at 14:14

2 Answers 2

3

children only selects the immediate children.

To disable all of selector's descendants, use this:

$('selector :not(a)').click(function(e) {
    e.preventDefault()
});

Or, to be a little more efficient, use delegation:

$('selector').on('click', ':not(a)', function(e) {
    e.preventDefault()
});
Sign up to request clarification or add additional context in comments.

Comments

0

When you want to disable the user from clicking stuff other than links, simply do this:

$('selector').find(':not(a)').click(function(e){
    e.preventDefault()
})

It applies to all elements other than links.

Comments

Your Answer

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