Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create link element with image as anchor with jquery?

I know how to create elements with jquery using something like:

$('<div/>').appendTo('body');

How can I create this:

<a href=""><img src="" /></a>

Using the same technique?

like image 375
user979390 Avatar asked Jan 16 '12 18:01

user979390


People also ask

How to add image in anchor tag in JQuery?

$('<a href=""><img src="" /></a>'). appendTo('body'); or $('<a href=""></a>'). append('<img src="" />').

How to add anchor tag in div?

You can now link to this section (div) using the anchor tag. To do that, just use the id of the section with a # as the prefix for the href value.


2 Answers

$('<img />').attr({
  src:'some image url',
  width:'width in intiger',
  height:'integer'
}).appendTo($('<a />').attr({
  href:'somelink'
}).appendTo($('#someElement')));
like image 108
dip1232001 Avatar answered Sep 27 '22 23:09

dip1232001


You can first select the html element using jquery and then use the "html()" method to set the desired html. Here is a sample:

$('div.demo-container')
  .html('<a href=""><img src="" /></a>');

The only thing is that you should be able to uniquely identify the desired div that you want to alter. Probably by setting id or class.

like image 31
anthares Avatar answered Sep 28 '22 00:09

anthares