Solution 1 :

To achieve this can create another element inside each td. The td will be used to display the square with the teal background. The inner element is necessary to show the circle with the yellow background. By default the circle can be hidden, and then displayed when the class is added to the parent td. Try this:

$(function() {
  $("td").click(function() {
    $(this).addClass('outpatient');
  });
});
table td {
  overflow: hidden;
  white-space: nowrap;
  border: 1px solid gray;
  text-align: center;
  cursor: pointer;
  background-color: aqua;
  padding: 0;
  margin: 0;
  position: relative;
}

td div {
  width: 32px;
  height: 32px;
  border: 1px solid transparent;
  line-height: 32px;
  margin: -1px;
  box-sizing: border-box;
}

td.outpatient div {
  background-color: yellow;
  border-radius: 50%;
  border-color: gray;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td><div>1</div></td>
    <td><div>2</div></td>
    <td><div>3</div></td>
  </tr>
  <tr>
    <td><div>4</div></td>
    <td><div>5</div></td>
    <td><div>6</div></td>
  </tr>
  <tr>
    <td><div>7</div></td>
    <td><div>8</div></td>
    <td><div>9</div></td>
  </tr>
</table>

Solution 2 :

You can consider background-image with radial-gradient to create the circle above the background-color

$(function() {
  $("td").click(function() {
    $(this).addClass('outpatient');
  });
});
table td {
  width: 20px;
  overflow: hidden;
  display: inline-block;
  white-space: nowrap;
  border: 1px solid gray;
  text-align: center;
  padding: 15px;
  cursor: pointer;
  background-color:aqua;
}
.outpatient {
  background-image:
    radial-gradient(farthest-side,yellow calc(100% - 3px),#000 calc(100% - 2px),transparent 100%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>

Problem :

I have a table and I would like to change a class on the td by clicking them. When I addClass() each cell changes but it seems override any class.

My desired result for each cell is like this:

enter image description here

How can I achieve this by adding a class to them?

$(function() {
  $("td").click(function() {
    $(this).addClass('outpatient');
  });
});
table td {
  width: 20px;
  overflow: hidden;
  display: inline-block;
  white-space: nowrap;
  border: 1px solid gray;
  text-align: center;
  padding: 5px;
  cursor: pointer;
  background-color: aqua;
}

.outpatient {
  background-color: yellow;
  border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
  </tr>
  <tr>
    <td>7</td>
    <td>8</td>
    <td>9</td>
  </tr>
</table>

Comments

Comment posted by Korovjov

Your class overrides TD state.

By