1

I am not sure on how to solve this problem in JavaScript, so I tought about "dynamic variables". If there is any better way to do it, please let me know!

Given is a function with one parameter (left or right):

var footerMenuLeftOpen = true;
var footerMenuRightOpen = true;

function footerMenu(menuName, className) {

  if (menuName) {
    $('.content-' + className).animate({
      height: "+=28px",
    }, {
      duration: 100
    });
    return false;
  } else {
    $('.content-' + className).animate({
      height: "-=28px",
    }, {
      duration: 100
    });
    return true;
  }
}


$(".content-left").click(function() {
  footerMenuLeftOpen = footerMenu(footerMenuLeftOpen, 'left');
});
$(".content-right").click(function () {
    footerMenuRightOpen = footerMenu(footerMenuRightOpen, 'right');
});
.content-left {
  width: 200px;
  height: 100px;
  background-color: red;
  display: inline-block;
}

.content-right {
  width: 200px;
  height: 100px;
  background-color: blue;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content-left">

</div>

<div class="content-right">

</div>

Now I want to add code, that the change in the height (+/- 28px) depends on the height of the red or blue box. Therefore I want to check for the starting height of the boxes as soon the boxes are displayed.

For example:

if ($('.content-left').height() > 42) {
    LeftHeight: "+=28px",
} else if ($('.footer-button-box.left').height() > 28) {
    LeftHeight: "+=14px",
} else {
    LeftHeight: "+=5px",
}

But how do I now change the height-value of the function "footerMenu" so that the height changes regarding to the determined values from the if-function?

I tought about something like

$('.content-' + className).animate({
      height: className+Height,
    }, {
      duration: 100
    });

... where className would be "left" plus the term "Height"; so leftHeight.

I guess there is a way better solution to make that work only one elegant function?

Thanks in advance!

EDIT: Since my question is hard to understand, in simple words:

I have variables "leftHeight" and "rightHeight". Now: how can I access these variables from a function where left/right is the value of the parameter?

Goal: height: leftHeight

Idea: height: className+"Height"

... so how to use the value of the parameter of the function as a part of a variable name?

2
  • if is not a function and doesn't return anything. Use ternary operators, and place them in the location of the property value. Commented May 30, 2017 at 13:01
  • Not sure what you are trying to do here, but if you want, you can take a look at flexbox, which I think might suffice your requirements.. without jS Commented May 30, 2017 at 13:01

1 Answer 1

1

I found the solution myself: I had to use objects.

var footerMenuHeight = new Object();

if ($(".content-left").height() > 40) {
  footerMenuHeight.left = 200;
} else if ($(".content-left").height() > 30) {
  footerMenuHeight.left = 18;
} else {
  footerMenuHeight.left = 8;
}

So I was able to use the parameter to access the value of this object:

footerMenuHeight[className]
Sign up to request clarification or add additional context in comments.

1 Comment

In JavaScript it's idiomatic to use {} instead of new Object().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.