Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 828027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:39:57+00:00 2026-05-15T03:39:57+00:00

It may seem a bit odd to ask this since there are several solutions

  • 0

It may seem a bit odd to ask this since there are several solutions out there but the fact is that all of them look pretty and none of what i’ve seem save the input value for form submission the right way.

I’m looking for something that will replace all radio inputs with divs that get special classes when they are hovered or clicked, and an input type hidden for every group of radio inputs with the same name, hidden input that will be updated with the value corresponding to the div the user clicks on. Long sentence, i know. Here’s what i’ve come up with:

$('input:radio').each(function(){
    if (this.style.display!='none') {
        var inputName = $(this).attr('name');
        var inputValue = $(this).attr('value');
        var isChecked = $(this).attr('checked');
        if (!$('input:hidden[name='+inputName+']').length)
        // if the hidden input wasn't already created
            $(this).replaceWith('<div class="inputRadioButton" id="'+inputName+'X'+inputValue+'"></div><input type="hidden" name="'+inputName+'" value="'+inputValue+'" />');
        else{
            $(this).replaceWith('<div class="inputRadioButton" id="'+inputName+'X'+inputValue+'"></div>');
            if (isChecked)
                $('input:hidden[name='+inputName+']').attr({'value':inputValue});
        }
        //this bind doesn't work
        $("#"+inputName+"X"+inputValue).click(function(){
            if($('input:hidden[name='+inputName+']').val()!=inputValue){
                $('input:hidden[name='+inputName+']').attr({'value':inputValue});
                $('div[id*='+inputName+'].inputRadioButton').removeClass('inputRadioButtonSelected');
            }
            if (!$("#"+inputName+"X"+inputValue).hasClass('inputRadioButtonSelected'))
                $("#"+inputName+"X"+inputValue).addClass('inputRadioButtonSelected');
        });
    }
});

Please tell me how to fix it. Thank you.

Edit I’ve found the reason. It should normally work but some of my radio inputs generated by an e-commerce software had brackets in them (e.g. id[12] ) and jQuery was parsing that. The fix is adding

var inputButton = document.getElementById(inputName+"X"+inputValue);

before the bind and replacing $("#"+inputName+"X"+inputValue) with $(inputButton).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:39:57+00:00Added an answer on May 15, 2026 at 3:39 am

    First of all, your deduction of the problem is pretty accurate. The [] characters aren’t legal in an HTML id, and if your input name has them, this code is going to break.

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (“-“), underscores (“_”), colons (“:”), and periods (“.”).

    But, here’s the thing. You don’t even need to assign any ID to the element, you can keep references to the create <div> and just reference it. Take a look at this refactored code:

    $('input:radio').each(function(){
      var $radio = $(this), radioName = $radio.attr('name');
      // check if the radio is shown:
      if ($radio.is(':hidden')) return; 
    
      // create a div
      var $div = $("<div class='inputRadioButton' />");
    
      // store the radio name on the div:
      $div.data('radioName', radioName);
    
      // look for the hidden already being present:
      var $hidden = $('input:hidden').filter(function() { 
        return this.name == radioName; 
      });
    
      if (!$hidden.length) {
        // didn't find the hidden, lets create one and append it to this div:
        $hidden = $("<input type='hidden' name='"+radioName+"' />").val($radio.val());
        $div.append($hidden); 
      }
    
      // if the radio is checked, set the hidden value:
      if ($radio.attr('checked')) {
        $hidden.val($radio.val());
        $div.addClass('inputRadioButtonSelected');
      }
    
      $div.click(function(){
        $hidden.val($radio.val());
    
        // find any selected radio divs with the same radioName as us
        // and remove the selected class
        $(".inputRadioButtonSelected").filter(function() {
          return ($(this).data('radioName') == radioName);
        }).removeClass('inputRadioButtonSelected');
    
        // add the class to our div:
        $(this).addClass('inputRadioButtonSelected');
      });
    
      $radio.replaceWith($div);
    });
    

    I put together a jsfiddle demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem a bit odd, but I really need to create a workaround
This may seem a bit crazy, but if you can tell me a better
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This question may seem a bit stupid, but i'm new to Joomla! And i
I'm sorry this may seem a bit basic, but I can't get my head
this may seem a little odd, but it would make for a convenient way
This may seem silly, but I'm a bit confused about the following code: public
This may be a stupid question but I can't seem to figure this out.
Ok this may seem like a bit of a noob question but one many
I understand that this question may seem somewhat ungrounded, but if someone knows anything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.