-2

In html there are many div classes with the same name "vlt-article-content". I am trying to insert image into class where the text is "Tim Pete - Search for Programmers"

Here is my jQuery:

$(".clickable-link").click(function () {
  $(this).closest('.vlt-article-content').children('h3 a').text('Tim Pete - Search for Programmers');
  $(this).prepend('<img id="theImg" class="logo" src="//i.stack.imgur.com/xY3PQ.jpg" />');
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="vlt-article-content">
  <h3 class="views-field-title clickable-link">
    <a href="/article/tim-pete-search">Tim Pete - Search for Programmers</a>
  </h3>
  <div class="vlt-article-meta">
    <div class="vlt-article-date">24.01.2018 </div>
  </div>
</div>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kontooo
  • 21
  • 1
  • 4
  • Please update the snippet I made with the event that executes the jQuery and change the HTML to what is before the code is executed. If you need to FIND the element that has the text, you cannot use .text() which will SET the content – mplungjan Jan 24 '18 at 09:08
  • The jqeury does not execute anything because I couldnt write it well. – Kontooo Jan 24 '18 at 09:17

1 Answers1

0

In jQuery, a new element can be created by passing a HTML string to the constructor, as shown below:

var img = $('<img id="theImg">'); //Equivalent: $(document.createElement('img'))
img.attr('src', "https://......");
img.appendTo('#vlt-article-content');
Léo R.
  • 2,620
  • 1
  • 10
  • 22
  • That was likely not the question. OPs image code would work – mplungjan Jan 24 '18 at 09:11
  • I can not edit the HTML, I am on the frontend side,the problem is img id is also same for all other div s on the page, the only thing that makes this content uniqe ,it is its text in a href. There fore I must insert the image by selecting this text. – Kontooo Jan 24 '18 at 09:17