Toggle the visibility of radio buttons by displaying a hidden div class instead. When the div is clicked, automatically select

I'm attempting to make this functionality work differently. Instead of displaying the standard radio button, or replacing it with an image, I want to use a styled div as a button. When hovered over, the class should change, and when clicked, the class should also change. Additionally, upon clicking, the hidden radio button should be selected. It's crucial that the label (referring to text like .co.uk, .com, .net, etc.) is inside the button div or that text can be added to the button. Is this achievable?


$(function() {
    $("input [name='domain_ext']").each(function() {
        if ($(this).prop('checked')) {
            $(this).hide();
            $(this).after($("< class='radioButtonOff' />"));
        } else { 
            $(this).hide();
            $(this).after($("<class='radioButtonOn' />"));
        }
    });

    $("input.radio").click(function() {
        if($(this).attr('src') == 'radiobuttonOn') {
            $(this).attr('src', 'radiobuttonOff');
            $(this).prev().attr('checked', 'false');
        } else { // its not checked, so check it
            $(this).attr('src', 'radioButtonOn');
            $(this).prev().attr('checked', 'true');
        }
    });
});

HTML

<table class="domain_ext_ribbon">
  <tr>
    <td><label>
      <input type="radio" name="domain_ext" value="all"  />
      All</label></td>
    <td><label>
      <input type="radio" name="domain_ext" value=".co.uk"  />
      .co.uk</label></td>
    <td><label>
      <input type="radio" name="domain_ext" value=".com" />
      .com</label></td>
  </tr>
  <tr>
    <td><label>
      <input type="radio" name="domain_ext" value=".net"  />
      .net</label></td>
    <td><label>
      <input type="radio" name="domain_ext" value=".org"  />
      .net</label></td>
    <td><label>
      <input type="radio" name="domain_ext" value=".biz" />
      .net</label></td> 
  </tr>
</table>

css

.radioButtonOff { width: 60px; height: 20px; background: #000000;}
.radioButtonHover { width: 60px; height: 20px; background: #666666;}
.radioButtonOn { width: 60px; height: 20px; background: #999999;}

Answer №1

Your selector seems to have a bug where the opening brace for the attribute filter is missing, causing an error.

$("input name='domain_ext']")

To correct this, it should be:

$("input [name='domain_ext']")

Unless you are specifically using jQuery version 1.6, your code should work fine. However, it is recommended to use

if ($(this).prop('checked'))

instead of

if ($(this).attr('checked'))

For more information, refer to this link.


Furthermore, the following line does not seem to make sense:

$(this).after($("<class='radioButtonOn' />"));

I believe you meant to write:

$(this).after($("<div class='radioButtonOff' />"));

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

display content from webpage using ajax with loading animation through dropdown selection

Looking to create a div that can have its content controlled through ajax load (with an added preloader just in case). Check out the fiddle I made here (my code is not the best). http://jsfiddle.net/ozzy/Vcwj5/2/ Can someone please clarify.. I want to ...

The jQuery autocomplete feature is not functioning properly when trying to separate values with commas using JavaScript and JSON

Currently, I am attempting to develop a comma-separated autocomplete text field where the autocomplete JSON data is generated directly from JavaScript. You can view my code below: The JavaScript array: var remark = [ "is under construction", "is ...

Button Hover Effect: Transition with Style

I am trying to implement a one-second transition from the default color scheme to the hover color for a Button element in Material UI. I am using the transitions.create(props, options) method as described in this article: https://medium.com/@octaviocoria/c ...

What are the steps to streamline the process of obtaining historical NASDAQ stock prices automatically?

Currently, I am manually obtaining raw data for 10-year historical daily prices for over 200 different tickers from . This process involves searching for each ticker, selecting '10 years' from the Timeframe drop-down menu, waiting for the respons ...

Troubleshooting a Peculiar Problem with Form Submission in IE10

Please take a look at the code snippet provided below: <!DOCTYPE html> <html> <body> <form name="editProfileForm" method="post" action="profileDataCollection.do"> <input type="text" id="credit-card" name="credit-card" onfocu ...

Incorporating PHP in JS/jQuery AJAX for creating unique odd and even conditional layouts

My PHP code includes a loop that changes the layout of elements based on whether the $count variable is odd or even. For odd counts, an image appears on the left and text on the right. For even counts, it's the other way around. To load content dynam ...

What is the art of spinning a div?

Looking to add an interactive circular div on my website that can be spun like a wheel of fortune using the mouse. I tried using jQuery drag without success in rotating the div. Is there a method to rotate div elements with the mouse? It would also be awe ...

Implement horizontal scrolling in a div using JavaScript or jQuery when clicked

After spending the past couple of days searching for a simple javascript or jquery code, I am determined to add a horizontal scrolling div to showcase images and descriptive text on the work page of my web portfolio. I came across a glide-onclick scrollin ...

Implement the Show/Hide TinyMCE Functionality in your <textarea></textarea> Box

Information I recently added TinyMCE to my website after discovering that a website I frequently visit uses it. While their design and functionality are different, I managed to replicate their look on my own site and now want to implement a specific featu ...

trouble with react-table's default height and flexible design

I am facing an issue with my React table configuration. I have set up scrollable columns and header following a similar example here: https://codesandbox.io/s/kowzlm5jp7?file=/index.js:281-310 In the table styles, I have specified a height like this: ...

Tips on how to adjust the HTML5 input type attribute in ios 5 to display the current date and time

I attempted the solution that seemed most straightforward, but unfortunately it didn't yield the desired outcome... const element = document.getElementById("element-id"); element.value = new Date().toString(); The code requires a string input, but h ...

Design a div in the shape of a trapezium with clipped overflow

Is there a way to create a trapezium using CSS/Html/Canvas? I have attempted to do so, but my current method is messy and not practical. div { width:0; margin-left:-1000px; height:100px; border-right:1000px solid lightblue; border-top:60px solid tra ...

Disappearing sticky-top navbar in Bootstrap when scrolling

I'm having trouble with the navbar on my website. Currently, it sticks to the top until I scroll out of the hero-image and then disappears. I would prefer the navbar to remain present throughout the entire page. Any suggestions on how to achieve this? ...

Enable checkbox selection on Vue.js table rows with a simple click

Having trouble making a list item clickable? Need the checkbox within the list item to be enabled or disabled on click, but it's not behaving as expected? Here is an example: var app = new Vue({ el: '#app', data: { showNav: ...

Unable to locate the value of the query string

I need help finding the query string value for the URL www.example.com/product?id=23 This is the code I am using: let myApp = angular.module('myApp', []); myApp.controller('test', ['$scope', '$location', '$ ...

Is there a way to decrease a field in a MongoDB database on a daily basis?

In the process of constructing an Angular2 application using MEAN stack architecture, I have a field called Remaining Days in my MongoDB database. I am interested in having this field automatically decrement by 1 each day. Do you know if this is possible ...

Uploading files in ASP.NET MVC without creating a view, utilizing Valums Ajax Uploader technology

I recently completed a tutorial on ASP.NET MVC file uploads using Valums plugin and made sure to place all the necessary js, css, and gif files in their respective folders. However, I am facing an issue where the view is not displaying anything. <link ...

In Typescript, how can we reverse the order of an enum

I am encountering a particular challenge with the following code snippet: enum MyEnum { Colors = 'AreColors', Cars = 'AreCars', } const menuTitle = ((obj: MyEnum) => { const newObj = {}; Object.keys(obj). ...

Running the Jcrop function multiple times within another function will not result in execution

When I click on the 'Edit A' link, a window opens where I can use Jcrop for editing. The process works smoothly for the first image. However, when I try to edit the second image using the 'Edit B' link, it keeps displaying the first ima ...

Unable to create a pie chart with chartjs using JSON data

I am having trouble creating a pie chart using canvas.JS. I have already tried iterating through them in a loop. The JSON data returned looks like this: [[{"label":"belum","x":1},{"label":"sudah","x":5}]] Below is the code I am using to retrieve the JS ...