Use jQuery to apply styles to all instances of the clicked element

For jQuery, I want to look at the element that I just clicked on (for example, ap or the li tag), and then apply the style to all instances of that element (so that all p tags are on the page).

This is my code so far, but it only applies the style to the only element that was clicked.

$("article *", document.body).click(function (e) {
  e.stopPropagation();
selectedElement = $(this);
$(selectedElement).css("border", "1px dotted #333")
});

Thank you for your help or advice!

+3
source share
2 answers
$('article *',document.body).click(
function(e){
    e.stopPropagation();
    var selectedElement = this.tagName;
    $(selectedElement).css('border','1px dotted #333');
}
);

Demo on JS Bin , although I used a universal selector (since I posted only a couple of lists (one from the olother a ul).

, @Peter Ajtai, JS Bin -, :

, tagName? var selectedElement = this.tagName;. e.stopPropagation(), .

+3

, article, *.

article tagName e.target, .

$("article", document.body).click(function ( e ) {
    e.stopPropagation();
    $( e.target.tagName ).css("border", "1px dotted #333")
});

.

+2

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


All Articles