Home > Blockchain >  Adding image before the "contains" using javascript
Adding image before the "contains" using javascript

Time:04-03

Need help on this code below. I don't want to replace the text on which I called in my "a:contains". The problem is where do I need to put the ::before on the script below.

Link: https://jsfiddle.net/onw7aqem/

$(document).ready(function() {
  $("a:contains(This is the text)").css("content", "url('https://i.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U')");
});
<a href="#">This is the text</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

CodePudding user response:

Consider the following.

$(function() {
  $("<div>").css("content", "url('https://i.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U')").appendTo($("a:contains(This is the text)"));
});
<a href="#">This is the text</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

This creates a <div> element and appends it to the <a> element.

  • Related