1

I am trying to execute a function with respect to the clicked element (here div with .redactor class).

If clicked the specified element (.redactor), then execute this function:

$('.redactor').on("click", function() {
    $(".redactor").each(function () {
        if($(this).hasClass("selected"))
        {
             destroy_redactor(current_edit);
             $(this).removeClass("selected");
        }
    });

    $(this).addClass("selected");
    current_edit = $(this);
    initialize_redactor(current_edit);
});

Or if clicked other than the specified element, execute this function if there is already an initialized redactor or else do nothing:

destroy_redactor(current_edit);

I saw this post as a reference. But it is not helping. It just removes all the divs.

I can't figure out how to execute the destroy_redactor(current_edit) function when clicked outside the .redactor div while there is already an initialized redactor for the current_edit.

Sample in codepen.io

Community
  • 1
  • 1
Kakar
  • 5,354
  • 10
  • 55
  • 93

1 Answers1

0

how about adding the click listener on the document...

$(document).click(function(event) {
  var target = $(event.target);

  if (target.attr('class') == 'redactor') {
     //do something with it
  }
  else{
     // clicked on none of the redactor elements 
  }
});

if you need to find if there is a redactor active you can do something like this

$(".redactor.active") //will get you a div with both redactor and active class
  • When clicked inside the `.redactor` div, its just taking the target as the child elements and not the `.redactor` div itself. How to correct this? Please could you check it with this [codepen.io](http://codepen.io/Kakar/pen/bVNEOy) – Kakar Sep 03 '15 at 09:46
  • if it returns a children of it (if you will click in the h1 it will return the h1 as target) you can check if the element is children of a redactor div like this if(target.parents(".redactor").length!=0) // ok i'm a redactor child else // i'm not a redactor child – gamera_the_beast Sep 03 '15 at 10:08
  • anyway if you try as i said you need to remove your $('.redactor').on('click') otherwise this will conflict with the $(document).click – gamera_the_beast Sep 03 '15 at 10:10
  • Yes now its differentiating between inside and outside nicely. But after removing the `$('.redactor').on("click")` , I get this error `Uncaught TypeError: Cannot read property 'createDocumentFragment' of null`. – Kakar Sep 03 '15 at 14:41
  • when are you getting this error? when you click something, when you load the page, or when? in the codepen.io i do not get that error – gamera_the_beast Sep 03 '15 at 15:54