Pawan_Agarwal Posted July 29, 2013 Share Posted July 29, 2013 I have this array that has n elements ---> list_array = new Array(); it contains ['Product 1','Product 2','Product 3','Product 4','Product 5','Product 6','Product 7','Product 8'] what I am trying to do is that starting a loop from j=0 to j<n, I am adding button on html page, now when I click on button, it must remove itself from the page. I want to do this with the help of javascript...Is there anyone who can help ??? If I click on "product 8", the page must display other 7 buttons and it must delete the button itself on the page..... function create() { for(j=0;j<i;j++) { "<button onclick='remove_me(this)' id="+list_array[j]+">Remove</button><br>"; } } function remove_me(obj) { _button = obj.id; remove(_button); } I am trying to perform this operation, however, it only receive "Product" in obj.id.............It skips the text after the first whitespace..... I hope that you got my concern, looking for quick reply....... Quote Link to comment https://forums.phpfreaks.com/topic/280613-how-to-add-and-remove-button-dynamically-with-javascript/ Share on other sites More sharing options...
nogray Posted July 29, 2013 Share Posted July 29, 2013 First, the id can't have any spaces, so you should remove the spaces from the ids. To remove the element, you should use the parent and removeChild, e.g. (your function example) function remove_me(obj) { obj.parentNode.removeChild(obj); } Quote Link to comment https://forums.phpfreaks.com/topic/280613-how-to-add-and-remove-button-dynamically-with-javascript/#findComment-1442599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.