Add button under div using the after() function

Asked

Viewed 465 times

0

Good afternoon!

I have the following situation:

Multiple Ivs, side by side, using display: inline-block.

I want, by clicking on a div, I can put a button underneath of this div.

I started using jQuery’s append function, as follows:

   $(".classeDaDiv").click(function() { 
      $(this).append("<button>Titulo</button>");
   })

No problem so far, you did what I wanted. The problem is that I need to remove this button by clicking again on the div. So I implemented that too. I am

1 answer

0

To insert an element after which it was clicked you must use the function insertAfter jQuery.

Change your code to the following:

$(".classeDaDiv").click(function() {
   $('<button>Titulo</button>').insertAfter(this);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.