Applying an id attribute based on a variable's value
Don't change IDs. IDs are supposed to identify a specific element, regardless of any state. "Active" should be a class.

The .mainmenu class should probably also be an ID. It's the "main menu" - that's its identity. Classes are for when you have multiple elements that are "of the same class", i.e. they share some functionality or styling. (If you have multiple things called "main menu", you have other problems.)

Using an active class will also let you use the IDs to find the link you want make active.

$("#mainmenu a.active").removeClass("active")
$("#" + menulink).addClass("active");


Basically: Find the currently active link, and remove the "active" class from it. Then find the link with the ID you're looking for, and set that to active by adding the class.