0

I have an image and I have a link. How can I use jQuery to make a linked image like this:
<a href="src"><img src="src"></a>

var image = $('<img>').attr('src', src);
var link = $('<a>').attr('href', src);
0

2 Answers 2

3

Use the append() method.

link.append(image);
Sign up to request clarification or add additional context in comments.

Comments

2

$(function () {
  var src = 'https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a';
  var href = 'https://stackoverflow.com/';
  
  var img = $('<img/>').attr('src', src);
  var link = $('<a/>').attr('href', href);
  
  // just set the content of the link to the image
  link.html(img);
  
  $('body').append(link);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

1 Comment

i like this solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.