I want to do the following:

  1. Learn the width of #sidebar and .main-content

  2. If the width of the #sidebar is less than the width of .main-content, assign a width from .main-content to it

  3. And if the width of #sidebar is greater than the width of .main-content, then for .main-content set the width from #sidebar.

I'm in jQuery for a couple of days, sorry for the stupid question and code.

function Equal() { var mainHeight = $(".main-content").height(); var sideHeight = $("#sidebar").height(); if (var sideHeight > var mainHeight) { $(".main-content").css("height", sideHeight); }; if (var sideHeight < var mainHeight) { $("#sidebar").css("height", mainHeight); }; }); } 
  • 3 hours ago something similar was already [here] [1] [1]: hashcode.ru/questions/320301/… - alvoro
  • if (var sideHeight> var mainHeight) {Writes without var. if (sideHeight> mainHeight) { - oldy

1 answer 1

First, you write what you want to check and change the width ( width ), and in fact you are trying to check and change the height ( height ).
Secondly, you need to learn the javascript syntax before you take on jQuery . Even in such a small example, you made a bunch of mistakes.
Thirdly, in this case, it is better to save the element itself in the variable, and not its height.

 var sidebar = $('.sidebar'); var content = $('.main-content'); if (sidebar.width() > content.width()) { content.width(sidebar.width()); console.log('--extending content'); } else { sidebar.width(content.width()); console.log('--extending sidebar'); } 

See an example

  • Thanks a lot , I was thinking about height, but I wrote about width: D - Prodius