Setting the delete button on javascript -


hello im trying write code add delete btn each input, add each 1 via javascript no problem :·

but delete btn not working @ all, think see error faster, cause im newbie on javascript. hope can me..

$(document).ready(function () {     var = $('.field').size() + 1;     $('#add').click(function () {         $('<div class="fieldss' + + '"><label for="link' + + '">video/link</label><input type="text" class="field' + + '" name="link' + + '" value="' + + '" /><a href="#" id="remove' + + '">remove</a></div>').fadein('slow').appendto('.inputs');         i++     });     $('#remove' + + '').click(function () {         $('.fieldss' + + '').remove();         i--     });     $('#reset').click(function () {         while (i > 2) {             $('.field:last').remove();             i--         }     });     $('.submit').click(function () {         var answers = [];         $.each($('.field'), function () {             answers.push($(this).val())         });         if (answers.length == 0) {             answers = "none"         }         alert(answers);         return false     }) }); 

the problem must in delete btn code because didnt know how set this... use have this, delete last item added , need delete btn each.

 $('#reset').click(function () {      while (i > 2) {          $('.fieldss:last').remove();          i--;      }  }); 

since adding dynamically can use event delegation binding event parent exists. use class remove add remove element. can use <div class="fieldss"> , then:-

$('#add')         .click(function () {         $('<div class="fieldss"><label for="link' + + '">video/link</label><input type="text" class="field' + + '" name="link' + + '" value="' + + '" /><a href="#" id="remove' + + '" class="remove">remove</a></div>')             .fadein('slow')             .appendto('.inputs');         i++;     });  $(document).on('click','.remove', function() {  // use container selector instead of document.     $(this).closest('.fieldss').remove(); }); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -