If condition in variable

Asked

Viewed 327 times

1

I have a variable called Lastclass, it can have the value of headerSortDown or headerSortUp. I’m rescuing her from an orderly column.

I want to transfer this variable to a <a href> but wanted to change her text.

For example instead of staying href="produto=headerSortDown" stick around href="produto=Down".

This variable changes dynamically as the user orders the DESC or ASC column. I can already dynamically rescue this.

There’s a way I could use one if or something like that to be able to change the name of the variable’s value and so put in the href?

2 answers

3


There is a lack of information to be able to give a well-adapted answer to your problem but, assuming that you only have two possibilities and assuming that you already have the variable you refer to headerSortDown or headerSortUp, logic can be like this:

var url = LastClass == 'headerSortDown' ? 'produto=Down' : 'produto=Up';
$('a').attr("href", url);

I suggest changing this variable to lastClass with small letter only because it is common practice to give large letter to Classes.

  • He said the variable is called Lastclass

  • @Lcsalazar ok, thank you, I changed the name. The logic is the same.

1

If you want the value headerSortDown turn Down, and the value headerSortUp turn Up, can use the function .replace() Javascript to remove the prefix of any value:

LastClass = LastClass.replace("headerSort", "");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.