There is such a column

<td id="sex">@person.Sex</td> 

Output there is 0 or 1 or even empty. What function should I write to replace, for example, 0 with the string "zero", 1 with the string "one", and the emptiness with "empty".

  • why do you not want to issue through select - Sergalas

1 answer 1

Made a div instead of td ... But the principle does not change :)

 $('.sex').each(function() { switch ($(this).html()) { case '0': $(this).html('ноль'); break; case '1': $(this).html('единица'); break; default: $(this).html('пусто'); break; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="sex">1</div> <div class="sex">0</div> <div class="sex"></div> <div class="sex">1</div> <div class="sex">0</div> 

  • And how for each element? - shatoidil
  • It changes only the first item in the column - shatoidil
  • In the task you wrote id="sex" , however id should be unique on the page. You should change the id to class . I changed the answer - look. - cyadvert