0

I am using jquery to find each target element in iframe on click event. But the click event triggers mutiple times on each click. This is the code that i used. I am using this function to style each target element on click. How can i solve this issue.

 var getElement = function () {
     $('[data-edit="froala"]').on('froalaEditor.initialized', function (e, editor) {
        var $div_tag = $('[data-edit="froala"]').find('iframe').contents().find('body');
            $div_tag.on('click', function(e) {
                var element_name = e.target.nodeName.toLowerCase();
                var $target_class = $('[data-target="'+element_name+'"]');
                trigger_object(e);
        });
     });
 }

 var trigger_object = function (e) {
   $('body').on('change', '[data-style="hr-style"]', function (event) {
       $(e.target).css($(this).data('css'),this.value);
   });
   $('body').on('change', '[data-style="div-style"]', function (event) {
       $(e.target).css($(this).data('css'),this.value);
   });
 }
5
  • Can you also add your html code? What is froala, and when do you trigger foalaEditor.initialized? Commented Oct 27, 2016 at 6:23
  • Check how often your froalaEditor.initialized is fired. You add every round an new click handler of $div_tag. Commented Oct 27, 2016 at 6:23
  • froala is a inline editor editor and froalaEditor.initialized is used to check froala is loaded Commented Oct 27, 2016 at 6:25
  • You are missing a . after find('iframe'), maybe a typo... Commented Oct 27, 2016 at 6:31
  • @pleinx thanks for the reply. Actually froalaEditor.initialized is working only once. Actually the issue happened due to the wrong way that i handle the target element. The issue is that when i click one div element in iframe i got the div target object and its wrking fine. Then i click on another div i got previos click div and current div object and its come like a loop Commented Oct 27, 2016 at 6:32

2 Answers 2

2

unbind your existing change event before binding it to prevent event from working multiple times

add .off('change') to unbind all exiting change event
before .on('change') to bind the change event

$('[data-style="hr-style"]').off('change').on('change', function (event) {
   $(e.target).css($(this).data('css'),this.value);
});
$('[data-style="div-style"]').off('change').on('change', function (event) {
   $(e.target).css($(this).data('css'),this.value);
});
Sign up to request clarification or add additional context in comments.

8 Comments

When i using the off event, its not working for [data-style="hr-style"] change method. Only second [data-style="div-style"] change method working. If I comment second [data-style="div-style"] change method, then first [data-style="hr-style"] change method working fine
Now its working fine. But is off method is a good way?. How can i do prevent multiple times in my code
.off() function is the best way to do that it unbind existing events from your element and prevent event working multiple times
its working fine when using $('[data-style="hr-style"]').off('change').on('change', function (event) {
@YeldhoPaul ah. I see my previous code only unbind the change event of body instead of [data-style="hr-style"] and [data-style="div-style"]
|
0

Then probably foralaEditor.initialized is fired multiple times.
Try with off, but on the div_tag click not on body,

var getElement = function () {
     $('[data-edit="froala"]').on('froalaEditor.initialized', function (e, editor) {
        var $div_tag = $('[data-edit="froala"]').find('iframe')contents().find('body');
            $div_tag.off('click').on('click', function(e) {
                var element_name = e.target.nodeName.toLowerCase();
                var $target_class = $('[data-target="'+element_name+'"]');
                trigger_object(e);
        });
     });
}

2 Comments

Thanks for the reply. Its not working. The change method inside trigger_object is working multiple times on each click in element in iframe. How can i solve this issue
@YeldhoPaul please add html code (where you use data-target) to your question.

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.