1

Is there a way I can set my height to be a percentage of the called width, by editing/adding to this js code? I want to do this in jQuery only, if possible.

var c1 = $('#gifcon').width();
$('#gifcon').css({
  'height': c1 + 'px' //i need height to be 35% percent of this
});
  • Does this answer your question? [Setting width and height as a percentage in JavaScript](https://stackoverflow.com/questions/18300408/setting-width-and-height-as-a-percentage-in-javascript) – helb Aug 25 '20 at 18:25
  • seems to be an X/Y problem. Possible duplicate of: https://stackoverflow.com/q/1495407/8620333 you can do what you want with only CSS – Temani Afif Aug 25 '20 at 19:17
  • @helb No, that wouldn't have answered my question, since I wanted to know if I could edit/add to this specific code snippet to achieve my desired result, but I do see how that definitely needed clarification :) – college student Aug 25 '20 at 21:15
  • @TemaniAfif No, that wouldn't have answered my question, since I needed to do this specifically only in jQuery, if at all possible. – college student Aug 25 '20 at 21:16
  • that's what we call an XY problem. What you are trying to do with jQuery can be done with CSS but you are not aware of – Temani Afif Aug 25 '20 at 21:20
  • @TemaniAfif I understand what you meant, but that isn't what's happening in my case. I already searched and saw that question before asking, but I couldn't find any solution that wouldn't mess with the rest of my code :( In my context, there wasn't a css solution that wouldn't make me re-do alot of work, so i needed to do this in jQuery. – college student Aug 25 '20 at 22:16

1 Answers1

1

You can change your code to this:

var c1 = $('#gifcon').width() * .35;
$('#gifcon').css({
    'height': c1 + 'px'
});

The number 0.35 is for convert your width to 35% of that.