-1

I would like to refactor my javascript code with an function.

ingos - is a name of the company and I have 5th of similar code parts.

    $('#ingosRegularPrice').removeClass("inactive");
    $('#ingosActivePrice').removeClass("inactive");
    $('#ingosRegularPrice .btn span').text(ingosPrice);
    $('#ingosActivePrice .btn span').text(ingosPrice * params.ingos.programB);

with something like

  function setPriceActive(company){
    $('#'+ company + 'RegularPrice').removeClass("inactive");
    $('#'+ company + 'ActivePrice').removeClass("inactive");
    $('#'+ company + 'RegularPrice .btn span').text(companyPrice);
    $('#'+ company + 'ActivePrice .btn span').text(ingosPrice * params.company.programB);
  }

But I get an reference error that 'company' variable doesn't exist. How I can use function params as an text reference and part of other variable name?

Thanks

1
  • You can use variable inside jQuery selector. Please verify company you are passing correctly or not Commented Feb 14, 2017 at 18:31

1 Answer 1

2

change:

params.company.programB

to:

params[company].programB

MDN Article on Object Notation

Sign up to request clarification or add additional context in comments.

4 Comments

Really? No duplicate worth posting? ;)
do you mean params[company].programB
All questions are duplicates in some way. However, it's clear with dot notations. Thanks. I just didn't know how the formulate the question on search. What about variable names? text(ingosPrice) . How do I use (companyPrice) instead?
Sergey, this[company + "Price"] should work. this would be the context depending where you declared the variable ignosPrice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.