How to target content of an element using jQuery?

I am having problems with a piece of code that I use to create a widget. see below:

$(".number-plate").html(function(i, h) { 
   return h.replace(/(ANY|[A-Za-z0-9])/g, '<img src="plate-widget/$1.gif" />'); 
});

The problem is that this replaces the whole element, not just the content!

Is there any way to customize the content .number-plateONLY?

Any help is much appreciated, thanks

+3
source share
3 answers

You can extract the node, modify it and subsequently replace it, for example:

var html = $(".number-plate").html();
$(".number-plate").html(html.replace(/(ANY|[A-Za-z0-9])/g, '<img src="plate-widget/$1.gif" />'));
+2
source

(edit :) If you have more than one element with a class number-plate, you can use:

$(".number-plate").each(function() {
   var $this = $(this);
   $this.html($this.html().replace(/(ANY|[A-Za-z0-9])/g, '<img src="plate-widget/$1.gif" />'); 
});
+2
source

, , .

, (h); . , , HTML...

0

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


All Articles