Resize image with JQ or JS [CLOSED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Resize image with JQ or JS [CLOSED]

How can set the size of an image after append, or once appended on the container? Thank you amigos!

11th Aug 2017, 4:29 PM
Kustaa
9 Answers
+ 1
var img = $('<img src="" alt="" />') img.height (100); img.width (150); $("#main").append (img) tu problema es que el height y width se lo seteabas al padre de la imagen. si el elemento ya existe en el documento, tenes que seleccionarlo y desp si hacerle un width o height. Si el elemento es siempre el mismo te conviene seleccionarlo una única vez y guardarlo en una variable, por que las búsquedas en el dom son operaciones muy costosas.
17th Aug 2017, 9:42 PM
Juan Altamirano
Juan Altamirano - avatar
+ 4
@David, ok. Thank you. I'll try that approach.
11th Aug 2017, 5:36 PM
Kustaa
+ 3
OK. So would be something like $("#main").append("<img src='img.jpg' alt='A'>").width(200).height(200); ?
11th Aug 2017, 4:51 PM
Kustaa
+ 3
Geez. This is my first serious question totally valid and on topic, and I get a downvote? I mean, don't upvote it, but what does the downvote means? It's a stupid question or you don't like me? So much for community.
11th Aug 2017, 4:58 PM
Kustaa
+ 3
no if ur doing that means ur changing the width and height of $("#main"). u need to do something like $("img")
11th Aug 2017, 5:33 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
$(selector).width() and .height() should work
11th Aug 2017, 4:43 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
Thanks, Juan. Got it. Any idea how to do it? I don't find it how, and want to implement it in my translator.
17th Aug 2017, 8:41 PM
Kustaa
+ 2
Holy molly Juan, that did. Gracias amigo!
18th Aug 2017, 2:55 PM
Kustaa
+ 1
primero seleccionaste al elemento con id main, desp le añadiste la imagen y el método retornó al elemento con id main, entonces le estas poniendo el width y height al contenedor de la imagen, no a la imagen en si.
15th Aug 2017, 2:29 AM
Juan Altamirano
Juan Altamirano - avatar