$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>underscore template dynamically remove row Jquery|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

underscore template dynamically remove row Jquery

matteradmin8PV0评论

I have this underscore template to add and delete row :

<#
  jQuery( document ).ready( function() {
   jQuery( '.add-ingr' ).click( function() {
    jQuery( '#re-ingr .ingr' ).append( '<div><input 
     type="text" name="ingr[]" value=""/><a href="#" 
     class="remove_field" style="margin-left:10px;">Remove</a><div>');
       });
    jQuery( '.remove_field' ).on("click", function(e){ 
      e.preventDefault(); jQuery(this).parent('div').remove();
       });
      });
        #>

the add row with remove link works , it generates the good html output:

   <div><input type="text" name="ingredients[]" value=""><a href="#" 
   class="remove_field" style="margin-left:10px;">Remove</a><div></div> 
   </div>

But not the remove , nothing happen when i click on the remove hyperlink Any idea pls

I have this underscore template to add and delete row :

<#
  jQuery( document ).ready( function() {
   jQuery( '.add-ingr' ).click( function() {
    jQuery( '#re-ingr .ingr' ).append( '<div><input 
     type="text" name="ingr[]" value=""/><a href="#" 
     class="remove_field" style="margin-left:10px;">Remove</a><div>');
       });
    jQuery( '.remove_field' ).on("click", function(e){ 
      e.preventDefault(); jQuery(this).parent('div').remove();
       });
      });
        #>

the add row with remove link works , it generates the good html output:

   <div><input type="text" name="ingredients[]" value=""><a href="#" 
   class="remove_field" style="margin-left:10px;">Remove</a><div></div> 
   </div>

But not the remove , nothing happen when i click on the remove hyperlink Any idea pls

Share Improve this question asked Mar 5, 2019 at 16:55 alpha.romeoalpha.romeo 491 gold badge1 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

This isn't WP related but Its not working because you are trying to add an event to a dynamically created element.

Replace...

jQuery( '.remove_field' ).on("click", function(e){ 
    e.preventDefault(); jQuery(this).parent('div').remove();
});

With

jQuery( 'body' ).on("click", '.remove_field', function(e){ 
    e.preventDefault(); jQuery(this).parent('div').remove();
});

You can read more about your issue here.

The div's you are trying to bind that event to don't exist when you create the listener. you need to use jQuery event delegation for this. like so:

jQuery( 'body' ).on("click", '.remove_field', function(e){
    e.preventDefault(); jQuery(this).parent('div').remove();
});
Post a comment

comment list (0)

  1. No comments so far