If condition in variable

1

I have a variable named LastClass , it can have the value of headerSortDown or headerSortUp . I'm rescuing it from an ordered column.

I want to transfer this variable to <a href> but wanted to change the text of it.

For example instead of getting href="produto=headerSortDown" getting href="produto=Down" .

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

How can I use a if or something like this to be able to change the value name of the variable and thus put in href ?

    
asked by anonymous 19.01.2015 / 19:35

2 answers

3

Some information is missing to be able to give a well-adapted answer to your problem, but assuming that you only have two possibilities and assuming you already have the variable that indicates whether it is headerSortDown or headerSortUp , logic can be :

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

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

    
19.01.2015 / 19:45
1

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

LastClass = LastClass.replace("headerSort", "");
    
19.01.2015 / 19:39