Jquery select any dom element on the page

Does anyone know how to select any element (possibly click) on the page, for example, select the body, select div, div # foo, etc., so I can put it in some kind of variable and edit it later.

I tried

$("*", document.body).click(function (e) {  
 e.stopPropagation();  
 var domEl = $(this).get(0);  
 alert("Clicked on - " + domEl.tagName);  
});

but it does not work for all elements

+3
source share
2 answers

You want to get the targetevent property :

$(document).click(function(e) {
   e.stopPropagation();
   var domEl = e.target; // or $(e.target) to jQuerytize it
   alert("Clicked on - " + domEl.tagName);
});

See: http://www.quirksmode.org/js/events_properties.html

Demo: http://jsfiddle.net/karim79/dyHEA/

+4
source

Try it.

$(function(){
    $("*").click(function () {
         console.log($(this));
         return false;
        }
    ); 
});
0
source

Source: https://habr.com/ru/post/1777621/


All Articles