0

Hello guys I'm havin some troubles with a a little program, I hope you can help me. The issue is that when I make a click in a div it suppose to add and other in the other box div, and it words fine but the problem is when I'm trying to get the index of those divs added it iterate over the all divs each time.

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
        <style>
            .contenedora {
                background-color: green;
                width: 300px
            }

            .contenedorb {
                margin-top: 10px;
                background-color: green;
                width: 300px
            }

            .dentro_contenedora {
                background-color: gray;
                height: 40px;
                margin-top: 5px;
            }

            .dentro_contenedorb {
                background-color: gray;
                height: 40px;
                margin-top: 5px;
            }
        </style>
        <script>
            $(function() { // INICIO JQUERY
                /*alert(2)*/
                $('.dentro_contenedora').click(function() {
                    $('.contenedorb').append('<div class="dentro_contenedorb">uno</div>')
                    $('.dentro_contenedorb').click(function() {
                        console.log($(this).index())
                    })
                })
            }) // FIN JQUERY
        </script>
    </head>

    <body>
        <div class="contenedora">
            <div class="dentro_contenedora">uno</div>
            <div class="dentro_contenedora">dos</div>
            <div class="dentro_contenedora">tres</div>
        </div>
        <div class="contenedorb">
        </div>
    </body>

</html>

This is the result when I click in them, it iterate each time

enter image description here

If I add directly in the console it words fine.

$('.dentro_contenedorb').click(function(){
    console.log( $(this).index() )
}
FloPinguin
  • 351
  • 1
  • 5
  • 16
El arquitecto
  • 513
  • 2
  • 5
  • 16
  • are you looking for something like this - https://codepen.io/nagasai/pen/RMRLNe – Naga Sai A Mar 15 '18 at 21:48
  • Thank you for answer, no this coude does exaclty the same as mine, when you make a click in the div added it iterate over the numer divs you added. if you in the console just type: " $('.dentro_contenedorb').on('click',function(){ console.log( $(this).index() ) })" it works but I need to added in the console by myself. – El arquitecto Mar 15 '18 at 23:05

0 Answers0