-4

I have this:

$(".nav").after("<a href=\"#\" class=\"lo\">LOGO</a>");

How can I replace the text "LOGO" to my image in an easy way?

Darko
  • 38,310
  • 15
  • 80
  • 107
Ja Pajoj
  • 3
  • 1

1 Answers1

3
  • Create your html image element
  • Set the source url of the image
  • Use the .after() method of jquery as you suggested. It can work with HTML objects as well.

Example:

var img = $(document.createElement('img'));
img.attr('src', "myimage.jpg");
$(".nav").after(img);

UPDATE with anchor

var img = $('<img />').attr({
  src:'myimage.jpg',
  width:'50',
  height:'10'
});
var anch = $('<a />').attr({
  href:'mydomain.com/page.html'
});
img.appendTo(anch);
$(".nav").after(anch);
Sam Segers
  • 1,951
  • 2
  • 22
  • 28
  • It works but img isn't a link :) How i can fix that? :) Thank You! – Ja Pajoj Dec 14 '15 at 01:35
  • I dont understant. My English is bad. How i can add a link to this image on this your code? – Ja Pajoj Dec 14 '15 at 01:43
  • Or you need to make it available somewhere that you can link to is, or you should encode your image in textual encoded representation like base64 as in the given link. There are a lot of websites doing this as well, like https://www.base64-image.de/ . – Sam Segers Dec 14 '15 at 01:46
  • No, i want my img href to home page – Ja Pajoj Dec 14 '15 at 01:51
  • I misunderstood you, updated the question with an example – Sam Segers Dec 14 '15 at 02:08
  • On no.. another problem... How i can add class to image because i need padding... – Ja Pajoj Dec 14 '15 at 02:42
  • Just add it with the other attributes? Try to experiment a little bit first please. You van also find these kind of examples elsewhere online if you like. – Sam Segers Dec 14 '15 at 02:44