Are you a member? Register or Login

Open Menu

How to Create Unique Block-Style Radio Inputs With jQuery

Input buttons are used in web forms where a user needs to select one option from a larger collection. This often happens with unique values like newsletter subscriptions, profile settings, and submission categories. I have always liked the old-school Digg-style input buttons where you click a link to choose your story category.

In this tutorial I want to demonstrate how we can build a similar interface using CSS3 and jQuery. All of the input radio buttons are still present within the form itself, but they are hidden on the page. Instead we update the selected choice using JavaScript and even have the possibility to display this value in HTML (or return it to a backend script). Check out my sample demo to get an idea for what we are building.

Live DemoDownload Source Code

Building the Webpage

First we need to download a local copy of jQuery and include a link to an external CSS document named styles.css. The JS code isn’t too complicated, but you could benefit by moving them into a separate .js file. It depends if your website requires additional scripting which would shave off time when rendering the page.

Now the inner page body contains a small number of important pieces. We have a span using the ID #radiovaltxt which is how I am displaying the selected input. Whenever a user clicks one of the page links, this span value gets updated with the currently-selected input value. Beneath this I’ve created a new form which doesn’t load anything – it is more of a placeholder to see how the HTML should look.

You’ll find the div #radiobtns contains a number of div rows which further contain unordered lists of elements. Each row is based on a single category topic and the user can select one of the options to mark the selected category. I only copied over one row of links but they are all structured very similar.

One important piece to understand is that the anchor link ID matches up to a corresponding input button value. This is how we can determine which link has been clicked and how this matches up with the radio selection. You will find these actual inputs located within a div #fullradios at the bottom of the form.

I have them hidden by default and the whole purpose is to never actually show them on the page. If users can interact with our own custom interface, then there is never any need for them to manipulate the radio inputs. Granted we could instead build the inputs linked to a label which doesn’t require any jQuery. But the effect is also more difficult to pull off and requires CSS3-compliant web browsers.

Styling the Cat Links

My default stylesheet includes all the typical resets and page structure as you would expect. The input fields are hidden using display: none by targeting the parent div element. Also the span text is designed to appear naturally centered towards the top of the page. You probably won’t use this on a live website but instead pass the value into the form using a hidden field.

Each row has a single unordered list and the very first item is given a class of .parent. This is how I’ve organized the list of categories. The parent item itself is not a link designated towards any input. You could change this in your own design, but it makes more sense to use a type of parent category label to define the choices. This parent item is given a fixed width of 110px to provide more space between the links.

I also wrote a notable class of .sel which is applied onto the selected link item. Once a user clicks one of the link values we need them to know it has been selected and confirmed. This selected class will be removed and re-applied onto new links as the user switches between categories.

Input Selection with jQuery

To manually select and deselect radio buttons you have to delve into jQuery a little bit. There are many popular questions on Stack Overflow which address this idea, and the techniques have changed over the various iterations of jQuery’s library. This script is located at the bottom of the index.html page and I’ll break it down into segments for easier comprehension.

Whenever a user clicks one of these links we immediately use event.preventDefault() passing in the click event variable. This stops the HREF value from loading so we don’t get the page jump associated with hashed links. The variable catid is a string formed in the syntax of a jQuery selector. It takes the link ID and re-forms it into the ID value of the corresponding radio input. The other variable checkedinput gets the value of the currently selected radio – if nothing is selected we get a return value of undefined.

So now we can break more into the logical code by first checking if there is nothing already selected. In this case we use jQuery’s .prop() method for setting the checked property. Otherwise we target every single radio using input[name=’category’] and remove the checked option so it reverts back to no selection. Then we update based on the most recently clicked radio link.

Afterwards I create another variable newval which again checks the currently-selected radio value. If this value matches the ID of the link then we know it has been successfully checked and we display the new value in HTML. This step probably won’t be useful on any live production website – but it’s nice for this demo to see how everything works behind-the-scenes.

This final chunk of code deals with updating the currently selected link. If the current link already has a .sel class then it’s already been selected and we do nothing. Otherwise it’s easier targeting each link inside #radiobtns and removing the class, to then update our current link using $(this).addClass(‘sel’).

Remember $(this) is merely a placeholder for any currently active jQuery selector, and in this case we’re talking about the click event which is performed on any of the category links.

Live DemoDownload Source Code

I can only imagine the plethora of solutions for why developers would use radio inputs. Modern web techniques offer so much in the way of user interface design, and this is an interesting way to customise a radio button experience into something completely unique.

I hope this tutorial may provide some ideas to web developers working with large forms and input fields. Also you can download a copy of my tutorial source code to see exactly how this works on your own computer.