javascript - Apply function to current click and remove function for other click -


trying apply function adddiv() current clicked element , it's working fine, it's not removing function adddiv() on click. when click on text, adddiv() function applied div tag , added sample text, need apply adddiv() function on current clicked tag. can me?

here link: https://jsfiddle.net/a0l2jj1y/1/

html

<div>     tesing 1 </div> <div>     tesing 2 </div> <div>     tesing 3 </div> 

js

$(function(){      function adddiv(){      $('<div class="txt"/>').text('sample text added').appendto('.clicked');      }      $(document).on('click', function(event){         var clickedtag = event.target         event.stoppropagation();         $('.clicked').removeclass('clicked');          if($(clickedtag).text().length >= 1){             $(clickedtag).addclass('clicked');             adddiv();         } else {             // need remove adddiv();         }     }); }); 

try:

$(function(){    function adddiv(){    $('<div class="txt"/>').text('sample text added').appendto('.clicked');    }   $(document).on('click', function(event){   var clickedtag = event.target   event.stoppropagation();   $('.clicked').removeclass('clicked');    if($(clickedtag).text().length >= 1){   $(clickedtag).addclass('clicked');   adddiv();    } else {   $(clickedtag).unbind();   }    });   }); 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -