1

I have animated datatables which slide in from the left when their hyperlink is clicked. And when the user is done reading the contents of the visible datatable, I applied the following code to allow the user to click anywhere else to park the table away and proceed with viewing. I used jQuery code for attaching the click event...

<script type="text/javascript" charset="utf-8">
    $(document).ready( function () {$('.dtable').dataTable( {"sDom": 'rt',"sScrollY":"200px", "bPaginate":false, "bFilter":false} );**$('body').click(function() {parkDataTables();});})
</script>

Unfortunately, clicking on the datatable itself parks it. And I don't want that behavior. Maybe someone has an idea on how to block this click event from firing on the surface of the datatable...

Many thanks

Dennis

1

3 Answers 3

2

Instead of using body as the selector you could use

$('body').children().not('.dtable')

So you would get

<script type="text/javascript" charset="utf-8">
$(document).ready( 
  function () {
   $('.dtable').dataTable( {"sDom": 'rt',"sScrollY":"200px", "bPaginate":false, "bFilter":false} );
$('body').children().not('.dtable').click(function() {
  parkDataTables();
 });})
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, i've used the NOT selector for something like this before, it should work very well.
This will only work if your dtable is an immediate child of the body.
Sounds right, but it still parks. Could it be that the DIV of the dTable is responding?
I implemented @Renato's 2nd suggestion. Thanks for your help!
no problem, as a side note, you could use $('body').find('*').not('.dtable'). Nice alternative @Renato :)
0

You should do it with

$('.dtable').click(function(){return false;});

The problem is that when you click on the table, the event goes into the table first and then it propagates to the parents (and finally to the body, when the parkDataTables is catched).

You could also use stopPropagation instead ( http://api.jquery.com/event.stopPropagation/ ), because with the return false you also stop the default click behavior on the table.

$('.dtable').click(function(event){
    event.stopPropagation();
});  

Maybe you could check this page to see this last difference: http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/

1 Comment

This one worked great and it was easy to implement. Many thanks for the help! Had to add the DIV to the table.. $('.dtable').click(function(event){event.stopPropagation()}); $('.ctnr').click(function(event){event.stopPropagation()});
0

Jquery propagates the function up through every parent. In order to stop the propagation, you must "return false;".

In your case, you want to try:

> $('body').click(function() { if $(this).hasClass('dtable'){return
> false;}) if $(this).hasClass('body'){park}

And give your body a class of 'body' to make it selectable.

2 Comments

Think it is better to get the selector right in the first place, as theoretically he now has two click events on the dtable class
Thank you for the help. Because of the added if statements I went with @Renato's second suggestion. It worked. Many thanks for your help.

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.