Detect if you click inside a Div

0

I am looking for a way to detect if click has been made within a div regardless of the content that is inside the div in question.

$(document).ready(function(){
$('#armaz  *').on('click', function()
{ alert($(this).prop('id')); });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="armaz" ><iframe>Contenido</iframe></div>

As you can see I can not make it work. I explain that the content within div is varied can be all kinds of objects, what I need is to detect if they have clicked the content of div no matter what the content is an example iframe.

    
asked by BotXtrem Solutions 14.07.2017 в 07:52
source

2 answers

3

It is not necessary to use the universal selector for this purpose. it would be enough just to listen to the event click for the tags div whatever the id or the content.

$(function() {
 $(document).on('click','div', function(){ 
  alert($(this).prop('id'));
 });
});
div{
    width: 100px;
    height: 50px;
    background :#ccc;
    margin:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="armaz" >Contenido</div>
<div id="armaz1" >Contenido</div>
<div id="armaz2" >Contenido</div>
<div id="armaz3" >Contenido</div>
  

As a recommendation you should read the on () documentation for   know how to use this method and what parameters you should send.

    
answered by 14.07.2017 / 08:19
source
2

Simply add the click event to the div

$( "#armaz" ).click(function() {
    alert( "Handler for .click() called." );
});
    
answered by 14.07.2017 в 07:57