0

I want click able buttons instead of radio buttons. I saw an example on jsfiddle but only problem is I don't have labels tags in my code. I don't have access to code, so I cant add them either. I can only add javascript and css to my CMS. Is there any way to achieve results?

<div class="button1">
    <input type="radio" checked="" value="1" name="question">question 1
</div>
<div class="button2">
    <input type="radio" value="2" name="question">question 2
</div>

I got solution here but its not working in IE.

$(function() {
    $('.container input[type="radio"]').each(function(index) {
        console.log($(this));
        $(this).attr('id', 'radio' + index);
        var label = $('<label />', {'for': 'radio' + index}).html($(this).parent().html());
        $(this).parent().empty().append(label);
    });
    $('label').click(function () {
       $('label').removeClass('selected');
       $(this).addClass('selected');
    });        
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
user3752575
  • 11
  • 1
  • 6

3 Answers3

1

To get this functionality, you have to wrap your inputs (and the description) in a label. Assuming that your radio is always in a seperate container, this is possible with this piece of code:

$('.container input[type="radio"]').each(function(index) {
    console.log($(this));
    $(this).attr('id', 'radio' + index);
    var label = $('<label />', {'for': 'radio' + index}).html($(this).parent().html());
    $(this).parent().empty().append(label);
});

working jsfiddle: http://jsfiddle.net/VyvpP/

Tobbe
  • 121
  • 5
  • You rock!! This worked.. but my div class is different for each radio ie button1, button2 ... how should i manage that? – user3752575 Jun 18 '14 at 13:44
  • If you change the first selector from `.container input[type="radio"]` to `input[type="radio"]` it will execute for all radios, independent of the class of the wrapping div. If you only want to convert some radios, but not all, change it to a comma separated list: `.container1 input[type="radio"], .container2 input[type="radio"]`. You can use every css valid selector you want, to select the radios. – Tobbe Jun 18 '14 at 13:50
  • For some reason selection process is not working in IE. Any suggestions? – user3752575 Jun 18 '14 at 14:40
  • if i hide radio button using display:none then IE is every times select 1st option only. – user3752575 Jun 18 '14 at 14:58
  • only if you hide the radio button? Because this works fine for me. Which version of IE do you use? – Tobbe Jun 18 '14 at 15:33
  • I can show you my site, but cant post user / pass here. Is there any way to send PM? – user3752575 Jun 20 '14 at 14:12
  • AS far as I know - no. But you can just simply answer my question: Which version of IE do you use? And which version of jQuery? – Tobbe Jun 20 '14 at 21:53
0

If, what I think you're asking is that you have a radio button in the guise of a clickable button, this should do you:

-webkit-appearance:none

http://jsfiddle.net/54ek6/

You can hide the radio button with -webkit-appearance:none; and using :before psuedo classes, add in the labels.

This does not work on IE. (Just so you know!)

wildandjam
  • 502
  • 2
  • 7
0

if you want something like this:

just copy and paste:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">   </script>
 <script>
$(document).ready(function(){
  $("#rad").click(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<input type="radio" checked="" value="1" name="question" id="rad">
</body>
</html>
gomzy
  • 250
  • 1
  • 2
  • 10