Omar Juvera
Omar Juvera

Reputation: 12287

jQuery: update an object's image source

On this sample fiddle, I failed to update the source of the object (image):

var image = $('<img />');
image.src = 'http://placehold.it/350x150';
image.appendTo( $('#test') );

Upvotes: 0

Views: 37

Answers (1)

Richard A
Richard A

Reputation: 2100

Since the image element is created via Jquery, image is a jquery object rather than a DOM element object. You should utilize jquery's attribute setter instead. see http://api.jquery.com/jQuery/#creating-new-elements

var image = $('<img />');
image.attr('src', 'http://placehold.it/350x150');
image.appendTo($('#test'));

or even better, set the property when creating the element:

var image = $('<img />',{src:'http://placehold.it/350x150'});
image.appendTo($('#test'));

Upvotes: 1

Related Questions