What event says that a radio button has been deselected within a radio input group?

Asked

Viewed 52 times

1

Knowing when a certain input within the group of radio inputs has been de-selected.

$('[name="teste"]').on('change', (e) => console.log(e))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span>1</span>
<input type="radio" name="teste" value="1" />
<span>2</span>
<input type="radio" name="teste" value="2" />
<span>3</span>
<input type="radio" name="teste" value="3" />
<span>4</span>
<input type="radio" name="teste" value="4" />

1 answer

1

You can use for this an event that happens before the change, which is the mousedown:

$('[name="teste"]').on('mousedown keydown', (e) => { 
  console.log("evento mousedown/keydown, valor atual: " + $('input[name="teste"]:checked').val());
});

$('[name="teste"]').on('change', (e) => { 
  console.log("evento change, novo valor: " + $('input[name="teste"]:checked').val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span>1</span>
<input type="radio" name="teste" value="1" />
<span>2</span>
<input type="radio" name="teste" value="2" />
<span>3</span>
<input type="radio" name="teste" value="3" />
<span>4</span>
<input type="radio" name="teste" value="4" />

  • 1

    well observed @Augustovasques. In that case, just treat tbm the keyboard event, keydown for example. I updated the code in the question, thank you for commenting

  • I had already left the +1. Thanks for the update.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.