javascript - How to change the default look of radio button, not functionality? - Stack Overflow

On a form I've two radio button Yes and No. I want to change the default look of radio buttons to

On a form I've two radio button Yes and No. I want to change the default look of radio buttons

to like this.

Edit: I would like to change the radio buttons into <a> anchor when JavaScript is enabled if it's not easy to change the look of default radio via css.

On a form I've two radio button Yes and No. I want to change the default look of radio buttons

to like this.

Edit: I would like to change the radio buttons into <a> anchor when JavaScript is enabled if it's not easy to change the look of default radio via css.

Share Improve this question edited Feb 25, 2011 at 18:09 Jitendra Vyas asked Feb 25, 2011 at 17:53 Jitendra VyasJitendra Vyas 153k240 gold badges587 silver badges868 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

Easiest way? Set the images as labels with the for attribute pointing to the radio buttons. Then set the radio buttons to display:none;

<input type="radio" name="radio[1]" /><label for="radio[1]"><img src="/yes.png"></label>
<input type="radio" name="radio[2]" /><label for="radio[2]"><img src="/no.png"></label>

p.s. This uses built in HTML functionality that works everywhere and doesn't require javascript. Use some CSS meta selectors (:hover etc...) to add animation.

UPDATE:

Just looping back after a long time and realised this could do with a little more explanation for beginners. When the for attribute is set on a label, clicking the label is functionally the same as clicking the element it's for attribute points to. For pleteness, the for attribute should be set to the id of the form element.

Rather than try to coerce the images to work with the <radio> tag, I would just put the images where you want them, bind the various behaviors to them using jQuery, and have them modify hidden input fields. I think it will be much easier that way.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745576161a4633973.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信