can1
can1

Reputation: 49

Do something if element equals value and is checked

I have 3 radio buttons and I would like to hide one of them. All 3 radio buttons have the same class but different values. I wrote code like this below but it's not working because the element is hidden on every radio button. Can you help me with this? Here is my code:

$(document).ready(function() {
  if ($('input.delivery_option_radio').val('49,').is(":checked")) {
    $(".hide").css("display", "none");
    alert("test");  
  }
});

Upvotes: 1

Views: 165

Answers (3)

Yahya Khalil
Yahya Khalil

Reputation: 24

if your sure that value in radio buttons is "49," not (49)

try this:

$('.hide[value="49,"]').hide();

else

$('.hide[value="49"]').hide();

Upvotes: 0

Mohammad
Mohammad

Reputation: 21489

Your code is wrong because you trying to set value of input to 49, and hide all .hide elements.

You don't need to if, just use Attribute Equals [name=”value”] and :checked selectors to do this work.

$(".delivery_option_radio[value='49']:checked").hide();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" class="delivery_option_radio" value="48">
<input type="radio" class="delivery_option_radio" value="49" checked>
<input type="radio" class="delivery_option_radio" value="50">

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

You're setting the value, not selecting. Try this in your if statement instead:

if($("input.delivery_radio_option[value=49]").is(":checked")) {...}

Upvotes: 1

Related Questions