If the element is checked and equal to a specific value, take action

I am trying to hide one of the 3 radio buttons on my page. Although they all have the same class, each button has a different value. I attempted to write code to achieve this, but unfortunately, it is hiding all radio buttons instead of just one. Could someone assist me in correcting my code? Here is what I have so far:

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

Answer №1

Instead of selecting the value, you are actually setting it. To fix this issue, consider updating your if statement to the following:

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

Answer №2

It appears that your code is incorrect because you are attempting to assign the value of an input to 49, while also trying to hide all elements with the class .hide.

To achieve this functionality, you can simply utilize the Attribute Equals Selector [name=”value”] and the :checked selector.

$(".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">

Answer №3

Make sure to check if the value in the radio buttons is precisely "49," instead of just 49.

Here is a solution you can try:

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

If not,

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Reorganizing Bootstrap 5 columns as the screen size changes

I need to change the display order of columns in bootstrap Currently I have two columns side by side in one row like this: Col A - Col B (on all screens except mobile devices) However, when the screen size is reduced or viewed on a mobile device, I want ...

Incorporate SCSS capabilities into a Vue project

The default content of my packages.json file includes the following: "postcss": { "plugins": { "autoprefixer": {} }} Whenever I try to compile using <style lang='scss'>, it doesn't work automatically like it does for Typescript. I ...

The Algolia Hit Component is having difficulty functioning properly within a grid layout

I am in the process of converting my next API to an Algolia search. The Hit component is a single component that renders for each record. However, I am facing an issue with implementing a grid layout. Below is the code snippet from before (which was workin ...

JavaScript code to calculate the total of elements in a multi-dimensional array

I am working on a JavaScript project where I need to calculate the sum of each subarray using forEach, map, and reduce functions. My goal is to have an output like this: sum = [8, 13, 22] However, despite my efforts, I cannot seem to get the desired outc ...

Regular expressions: understanding greedy versus lazy quantifiers

Imagine this situation: a = 'one\\two\\three.txt'; The desired output is "three.txt". However, the attempted solution of: a.match(/\\(.+?)$/) is unsuccessful. What could be causing this issue? How can we successf ...

What steps should be taken to address the issue when the .media$thumbnail.url cannot be located

Creating a custom widget for bloggers <script type="text/javascript"> function mycallback(jsonData) { for (var i = 0; i < jsonData.feed.entry.length; i++) { for (var j = 0; j < jsonData.feed.entry[i].link.length; j++) { ...

Using jQuery to create clickable URLs within a rollover div

I am looking to have the div appear on a mouse over effect in the following code. Is there a way for me to input a url based on the data that is passed to it? Anchorage: ["Anchorage", "(555)555-5555"], (This represents the data being posted) AtlanticCit ...

Enhance your input text form with a stylish button using Bootstrap

This is the code snippet I am currently working on: <h2>Workout Selector</h1> <div class="form-group"> <label for="workout-name">Search by Keywords (Comma Separated):</label> <input ...

sending data from angular controller to a node server

Having trouble with my Angular controller that posts to the node server. The function triggering the post is giving me a 404 (Not Found) error when running it. I've tried rewriting the post multiple times but can't seem to locate the issue. If ...

I have been experiencing issues with browserify when trying to use both jquery and jquery-ui

I have created a simple test app to showcase an issue I am facing. You can find the demo on this GitHub repository. In this demonstration, the first step was installing jquery and jquery-ui: npm install --save jquery jquery-ui Then I proceeded to creat ...

transmitting data in the form of an array using

This is the code I am currently using to display the text of the selected option in the post: $("#major_names").change(function () { $.post('find_lesson.php', { dars:$("#major_names option:selected").text() }, function(data){ ...

Making a column in a Vue data grid return as a clickable button

My goal is to utilize vue.js grid to display multiple columns with calculated text values, along with a clickable column at the end that triggers a dynamic action based on a parameter (such as calling an API in Laravel). However, when I include the last c ...

Creating a photo grid with consistent order is simple using flexbox

I'm currently working on an image grid that consists of 2 rows laid out horizontally. Initially, the first row contains 4 images while the second row has 2 images. Users should have the ability to add more images to the second row. The issue I am faci ...

Requesting the user to repeatedly input their birth year until it is less than the current year

Can anyone help me figure out how to display a prompt until the user enters a birth year that is less than the current year? I've tried using a loop in my code, but I'm having trouble getting it right. Any assistance would be greatly appreciated. ...

Loop through each JSON object and insert it into an HTML element

I am working on looping through a JSON object and converting it into HTML. While I can iterate through all the objects in the JSON, I am having trouble extracting just the first object. var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback ...

Tips for enhancing the functionality of components within Vue

My expertise lies primarily in React, and I'm now exploring the Vue-centric approach to achieve the following: I want to enhance this component: , so that the label-position is set to top on mobile devices and left on desktop. However, I'm not s ...

Issue with the functionality of Material-ui tooltip

I'm exploring the implementation of the material-ui tooltip feature and I am hoping to have it displayed at the top of the element. Despite specifying placement="top", the tooltip still does not appear as intended. You can view a demo of this issue b ...

Utilizing Vue.js: Disabling button on image carousel when there is no "next" photo accessible

This is my initial experience with Vue. I am attempting to assemble a slideshow using an array of images. I have successfully managed to disable the "previous" button when the user reaches the beginning of the slideshow, but I am encountering difficulties ...

What is the best way to retrieve data from the next page in a Protractor test?

I am currently in the process of automating the test for booking a flight. When I enter the credentials on the homepage, click the Submit button, and the browser navigates to the search results page. const EC = protractor.ExpectedConditions; describe( ...

React Native ScrollView ref issue resolved successfully

I'm trying to automatically scroll to the bottom of a flatlist, so here's what I have: const scrollViewRef = useRef(); //my scroll view <ScrollView ref={scrollViewRef} onContentSizeChange={() => { scrollViewRef.current.scr ...