Cómo seleccionar el ancho de la imagen src - Jquery

How do I select the width of selected image on choose file?

if(new Image().attr('src').width() < 200){
alert('Expected');

}else{

alert('Woah?');

}

preguntado el 09 de septiembre de 13 a las 23:09

What do you mean by "selected image"? You've provided some invalid code. What have you actually attempted so far? -

Yes, the code doesn't seem to be working. '<input type="file">' selected files width is what I want to detect. -

<input type="file">* -

You're going to need to do a better job of explaining what you want. Take it a step at a time, and spell it out in your question. The code you provided is an odd mix of function calls that will simply fail. -

You can't check the width until the image is uploaded. -

1 Respuestas

To select the source and width of an image using Javascript (not jQuery specific):

Aquí hay un jsFiddle, and here's the js code:

var test_image = document.getElementById("TestImage");
var test_image_src = test_image.getAttribute("src"); // get the source
var test_image_width = test_image.getAttribute("width"); // get the width

if(test_image_width < 200){
    alert('Expected');
}else{
    alert('[Woah]?');
}

You can retrieve any attribute using this method.

var attribute = element.getAttribute(attributeName);
https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute

Respondido el 10 de Septiembre de 13 a las 00:09

Thanks very much, I was confused as whether to use jQuery or Pure js Thanks again! - Shogg

This answer wan't exactly fit for the <input> part of the question. However, I have implemented my own code. THAT WORKS :D - Shogg

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.