How to append image to the link by javascript

Posted on 2009-07-05
Last Modified: 2012-05-07
Hi I've got to add picture to the side of a link.

My question is how do I append image tag to the link via javascript?

the link looks like this:
 <li id="35">
<a href="?address=1"> link </a>
how do i append: <img src="..." /> just after <li>
(I want it to be appended straight after document is ready for display)
Question by:vidda22

Accepted Solution

stevepicks earned 200 total points
Comment Utility
 this script is the closest i could think:
1) edit : images[1].src = "images/image1.gif";  to point to a real image
2) get a clear.gif image that is transparent

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">



<!-- Begin


images = new Array;

images[1] = new Image();

images[1].src = "images/image1.gif";

images[2] = new Image();

images[2].src = "images/image2.gif";

images[3] = new Image();

images[3].src = "images/image3.gif";

images[4] = new Image();

images[4].src = "images/image4.gif";

images[5] = new Image();

images[5].src = "images/image5.gif";

images[6] = new Image();

images[6].src = "images/image6.gif";

function GetPic(num) { 

document.ImageHolder.src = images[num].src;

document.ImageHolder.width = images[num].width;

document.ImageHolder.height = images[num].height;


// End -->


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>


<body onLoad="GetPic(1);">

<li id="35">

<img name="ImageHolder" src="images/clear.gif" width="1" height="1" border="0" alt="some text">

<a href="?address=1"> link </a>




Open in new window

LVL 31

Expert Comment

Comment Utility
<a href="#" onclick=document.img1.src=";">Link</a>


<img src ="http://a.gif" name="img1"><p>

Open in new window

LVL 31

Expert Comment

Comment Utility
sorry there's a typo above (although strangly it works?). It should be

<a href="#" onclick='document.img1.src=";"'>Link</a>
<img src ="http://a.gif" name="img1">
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

LVL 10

Assisted Solution

Bane83 earned 100 total points
Comment Utility
Use jquery for this. (
$(document).ready(function () {

    $("li > a").prepend("<img src="..." />");


Open in new window

LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 200 total points
Comment Utility
or without jQuery

window.onload=function() {
  var html =document.getElementById('35').innerHTML;
  if (html.toLowerCase().indexOf('img')==-1)
    document.getElementById('35').innerHTML = '<img src="..." />'+html;
LVL 10

Expert Comment

Comment Utility
That assumes a single li of id='35'.


items = document.getElementsByTagName('li') OR containingListElement.getElementsByTagName('li');
for(i = 0; i < items.length; i++)
     item.innerHTML = '<img src="..." />' + item.innerHTML;

I just prefer using jquery since it generally works in any browser you throw at it.

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now