I'll assume you've probably actually got more than one character. Therefore it might make sense to maintain a list of characters in a single object. Each character's statistics would be a property of that object. That in turn would make it possible to dynamically reference the relevant property in the way you're imagining.
So let's say you've got a characters
object like this:
var characters = {
"marri": { selected: 0},
"someOtherCharacter": { selected: 0 }
}
You could get the character based on its class just as you wrote:
characterName = $(".units a").attr("class");
(Or, if you're responding to a click on that element, it might actually be characterName = $(this).attr("class");
.)
...and then use that character
value to refer to the property in your object using bracket notation:
characters[characterName].selected = 1;
Demo:
var characters = {
"marri": {
selected: 0
},
"someOtherCharacter": {
selected: 0
}
}
$(function() {
$(".units a").click(function(e) {
e.preventDefault();
characterName = $(this).attr("class"); //slightly different to the above example - this directly gets the class of the clicked link
characters[characterName].selected = 1;
console.log(characters); //just to show the object has been updated
});
});
.units a
{
padding: 5px;
border: 1px solid #cccccc;
margin: 1px;
}
.units a:hover
{
cursor:pointer;
background-color: #cccccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="units">
<a class="marri">Marri</a>
<a class="someOtherCharacter">Some Other Character</a>
</div>