I'm new to frontend development, and I want to rezize an <img>
with JavaScript:
Which element?
<div class="dz-image">
<img data-dz-thumbnail="" alt="principal.png" src="http://localhost:49407/file/Image/660">
</div>
What function im using in js?
this.emit("thumbnail", mockFile, "http://localhost:11111/file/Image/" + principal)
{ document.querySelector('data-dz-thumbnail') };
I'm using querySelector
to get the spefic <img>
element.
Is it possible to add some style directly on that line like height:120; width: 120;
?
asked Apr 1, 2020 at 18:15
3 Answers 3
Yes,it is possible to add some style directly.
let ele = document.querySelector('[data-dz-thumbnail]')
ele.style.width="120px"
ele.style.height="120px"
3 Comments
gaccerboni
It looks like the correct answer, however img size still the same. (im not sure if this is for another reason)
Vimal
It is working for me. can host your code in jsfiddle or some where @gaccerboni
gaccerboni
This fix my problem on the img but changes all the others that shouldnt: 'code' $('div.content img').css('width', '120'); $('div.content img').css('height', '120'); 'code'
Per my understanding, queryselector
should be classname
, id or tag
Second you can add css by taking a variable for your querySelector
line. It is like:
var el = document.querySelector('div');
el.style.backgroundColor = 'green';
el.style.display = 'none';
el.style['border-radius'] = '5px';
answered Apr 1, 2020 at 18:26
Comments
I solved this using jQuery's .css()
method like this:
$('[data-dz-thumbnail]').css('width', '120');
$('[data-dz-thumbnail]').css('height', '120');
answered Apr 1, 2020 at 18:54
Comments
lang-js
this.emit
&mockFile
here?document.querySelector('data-dz-thumbnail')