Q:
How to create custom radio buttons in HTML using CSS and jQuery
belongs to collection: JavaScript / jQuery Frequently Asked Questions
JavaScript / jQuery Frequently Asked Questions
- How to write comments in JavaScript
- How to show and hide div elements based on the selection of radio buttons in jQuery
- How to show and hide div elements based on the click of checkboxes in jQuery
- How to show and hide div elements based on dropdown selection in jQuery
- How to define a function in jQuery
- How to define a function in JavaScript
- How to call a function automatically after waiting for some time in jQuery
- How to add CSS properties to an element dynamically using jQuery
- How to add attributes to an HTML element in jQuery
- How to remove the attribute from an HTML element in jQuery
- How to add new elements to DOM in jQuery
- How to remove elements from DOM in jQuery
- How to remove white spaces from a string in jQuery
- How to find the number of characters in a string using jQuery
- How to remove the clickable behavior from a disabled link using jQuery
- How to create custom radio buttons in HTML using CSS and jQuery
- How to create custom checkboxes in HTML using CSS and jQuery
- How to create custom select box in HTML using CSS and jQuery
- How to create a string by joining the elements of an array in JavaScript
- How to check a checkbox is checked or not using jQuery
- How to get the value of selected radio button in a group using jQuery
- How to get the values of selected checkboxes in a group using jQuery
- How to bind click event to dynamically created HTML elements in jQuery
- How to set the width of a div element dynamically using jQuery
- How to set the height of a div element dynamically using jQuery
- How to remove wrapper element but keep the text content intact using jQuery
- How to animate a div height based on the content using jQuery
- How to highlight alternate table row using jQuery
- How to get the current page url using jQuery
- How to calculate the number of words in a string using jQuery
- How to find substring between the two words using jQuery
- How to Get the Value of Selected Option in a Select Box Using jQuery
- How to get the text inside an element using jQuery
- How to check an element is visible or not using jQuery
- How to populate state dropdown based on option selected in country dropdown using jQuery
- How to get the position of an element relative to the document using jQuery
- How to get the position of an element relative to the parent using jQuery
- How to find mouse position relative to the document using jQuery
- How to find mouse position relative to an element using jQuery
- How to get substring from a string using jQuery
- Show read more link if the text exceeds a certain length using jQuery
- How to get current image size (width & height) in javascript
- How to get original image size (width & height) in JavaScript
- How to increase and decrease image size using JavaScript
- How to create jQuery slide up and down toggle effect
- How to create jQuery slide left and right toggle effect
- How to animate div height on mouse hover using jQuery
- How to animate div width on mouse hover using jQuery
- How to play and stop CSS animation using jQuery
- How to disable or enable a form element using jQuery
- How to disable all input controls inside a form using jQuery
- How to check or uncheck radio button dynamically using jQuery
- How to remove first character from a string in jQuery
- How to stop firing event until an effect is finished in jQuery
- How to check if an element exists in jQuery
- How to move an element to left, right, up and down using arrow keys in jQuery
- How to animate the opacity of an element on mouse hover using jQuery
- How to hide dropdown menu on click outside of the element in jQuery
- How to toggle text inside and element on click using jQuery
- How to customize file input type box using CSS and jQuery
- How to fire event on file select in jQuery
- How to get selected file name from input type file using jQuery
- Auto update div content while typing in textarea using jQuery
- How to insert HTML content into an iFrame using jQuery
- How to call a function repeatedly after fixed time interval in jQuery
- How to Redirect to Another Web Page Using jQuery or JavaScript
- How to check if an element is hidden using jQuery
- How to select all visible or hidden elements in a page using jQuery
- How to add or remove rows inside a table dynamically using jQuery
- How to move an element into another element using jQuery
- How to change href attribute of a hyperlink using jQuery
- How to refresh a page with jQuery
- How to select an element with multiple classes with jQuery
- How to loop through an array in JavaScript
- How to replace character inside a string in JavaScript
- How to explode or split a string in JavaScript
- How to assign block of HTML code to a JavaScript variable
- How to display all items or values in an array using loop in jQuery
- How to get number of elements in a div using jQuery
- How to replace all occurrences of a string in JavaScript
- How to check whether a value is numeric or not in jQuery
- How to replace multiple spaces with single space in JavaScript
- How to detect click inside iframe using JavaScript
- How to determine if variable is undefined or null in JavaScript
- How to get element by class name in JavaScript
- How to detect enter key press on keyboard using jQuery
- How to detect if enter key is pressed in a text input field using jQuery
- How to change the background color of a web page dynamically using JavaScript
- How to check if a variable exists or defined in JavaScript
- How to get the value of a textarea in jQuery
- How to make the first letter of a string uppercase in JavaScript
- How to Create Multiline Strings in JavaScript
- How to Check Whether a String Contains a Substring in JavaScript
- How to Encode URL in JavaScript
- How to Remove Duplicate Values from a JavaScript Array
- How to Remove a Property from a JavaScript Object
- How to Check If a Value Exists in an Array in JavaScript
- How to Get Day, Month and Year from a Date Object in JavaScript
- How to Remove a Specific Item from an Array in JavaScript
- How to Convert Comma Separated String into an Array in JavaScript
- How to Get the Class Name of an Object in JavaScript
- How to Check for an Empty String in JavaScript
- How to Sort an Array of Integers Correctly in JavaScript
- How to Return Multiple Values from a Function in JavaScript
- How to Get the Current URL with JavaScript
- How to Include a JavaScript File in another JavaScript File
- How to Detect Screen Resolution with JavaScript
- How to Parse JSON in JavaScript
- How to Split a String into an Array of Characters in JavaScript
- How to Dynamically Access Object Property Using Variable in JavaScript
- How to Append Values to an Array in JavaScript
- How to Generate a Timestamp in JavaScript
- How to Convert JS Object to JSON String
- How to Add New Elements at the Beginning of an Array in JavaScript
- How to Get the Value of Text Input Field Using JavaScript
- Automatically Adjust iFrame Height According to its Contents Using JavaScript
- How to Call Multiple JavaScript Functions in onClick Event
- How to Store JavaScript Objects in HTML5 localStorage
- How to Get Portion of URL Path in JavaScript
- How to Add a Class to a Given Element in JavaScript
- How to Find the Max and Min Values of an Array in JavaScript
- How to Check If an Object Property is Undefined in JavaScript
- How to Capture Browser Window Resize Event in JavaScript
- How to Reset a Form Using jQuery or JavaScript
- How to Detect a Click Outside of an Element with jQuery
- How to Detect Change in a Text Input Box in jQuery
- How to forEach Over an Array in JavaScript
- How to Check If an Input Field is Empty Using jQuery
- How to Get the Children of the $(this) Selector in jQuery
- How to Check If the Mouse is Over an Element in jQuery
- How to Scroll to the Top of the Page Using jQuery/JavaScript
- How to Insert an Item into an Array at a Specific Index in JavaScript
- How to Get the ID of the Element that Fired an Event in jQuery
- How to Find the Sum of an Array of Numbers in JavaScript
- How to Check If an Array Includes an Object in JavaScript
- How to Submit a Form Using jQuery
- How to Create a DIV Element in jQuery
- How to Convert a JS Object to an Array Using jQuery
- How to Change the Image Source Using jQuery
- How to Select an Element by Name in jQuery
- How to Find an Element Based on a Data-attribute Value in jQuery
- How to Set CSS background-image Property Using jQuery
- How to Detect a Mobile Device in jQuery
- How to Get the data-id Attribute of an Element Using jQuery
- How to Check If Object is an Array in JavaScript
- How to Remove an Event Handler in jQuery
- How to Get the ID of an Element using jQuery
- How to Add Options to a Select Box from a JS Object using jQuery
- How to Replace innerHTML of a Div using jQuery
- How to Add li in an Existing ul using jQuery
- How to Get the Value in an Input Text Box using jQuery
- How to Change CSS display Property to none or block using jQuery
- How to Set the Value of Input Text Box using jQuery
- How to Change the Text of a Button using jQuery
- How to Loop Through Elements with the Same Class in jQuery Topic: JavaScript / jQuery
- How to Allow Only Numeric Value in HTML Text Input Using jQuery
- How to Check for a Hash (#) in a URL using JavaScript
- How to Convert a Float Number to a Whole Number in JavaScript
- How to Fix Uncaught ReferenceError: $ is not defined in jQuery
- How to Get Class List of an Element with jQuery
- jQuery $(document).ready() Equivalent in JavaScript
- How to Set or Unset a Cookie with jQuery
- How to Show Loading Spinner in jQuery
- How to Preview an Image Before it is Uploaded Using jQuery
- How to Get Class Name Using jQuery
- How to Sleep Before Continuing in JavaScript
- How to Set Value of Textarea in jQuery
- How to Convert UTC Date Time to Local Date Time in JavaScript
- How to Load Local JSON File Using jQuery
- How to Trigger a Click on a Link Using jQuery
- How to Get the Class of the Clicked Element in jQuery
- How to AJAX Submit a Form in jQuery
- How to Count Number of Rows in a Table Using jQuery
- How to Merge Two Arrays in JavaScript
- How to Check If a Value is an Object in JavaScript
- How to Loop Through a JavaScript Object
- How to Display a JavaScript Object
- How to Add a Key/Value Pair to an Object in Javascript
- How to Test For an Empty Object in JavaScript
- How to Check If a Key Exists in a JavaScript Object
- How to Sort an Array of Objects by Property Values in JavaScript
- How to Merge the Properties of Two JavaScript Objects
- How to Get the Length of a JavaScript Object
- How to Check If an Element Contains a Class in JavaScript
- How to Create a Two Dimensional Array in JavaScript
- How to Empty an Array in JavaScript
- How to Get Query String Parameters Values in JavaScript
- How to Remove Empty Elements from an Array in JavaScript
- How to Stop setInterval() Call in JavaScript
- How to Trigger a Button Click on Enter Key Press in a Text Box in JavaScript
- How to Get the Last Item in an Array in JavaScript
- How to Get the Current Date in JavaScript
- How to Convert a String to Boolean in JavaScript
- How to Format a JavaScript Date
- How to Parse a String to a Date Object in JavaScript
- How to Find an Object by Property Value in an Array of JavaScript Objects
- How to Get Month Name from a Date in JavaScript
- How to Convert Decimal Values to Hexadecimal in JavaScript
- How to Check If a Variable is a String in JavaScript
- How to Set Default Parameter Value for a JavaScript Function
- How to Add Days to Current Date in JavaScript
- How to Compare Two Dates in JavaScript
- How to Convert a Unix Timestamp to Time in JavaScript
- How to Copy Array by Value in JavaScript
- How to Remove Text from a String in JavaScript
- How to Check If an Array Exists and Not Empty in JavaScript
- How to Check If an Array Exists and Not Empty in JavaScript
- How to Get a Random Item from a JavaScript Array
- How to Format JavaScript Date as YYYY-MM-DD
- How to Generate a Random Number between Two Numbers in JavaScript
Use the CSS
:checked
Pseudo-class with jQueryIf try to style radio buttons directly using CSS properties like
background
orborder
it will not work, because most of the form elements are native part of the browsers and does not accept so much visual styling. So we have to find another way of styling radio buttons.In this tutorial we will create custom radio buttons with the help of CSS and little bit of jQuery. There are many other solutions available like jQuery plug-ins to customize radio buttons but they are unnecessarily complex and not so easy to implement. Here we will simulate the effect of custom radio buttons with the minimum effort that works like a charm and very easy to implement in any project. It is highly compatible across browsers, even works perfectly in Internet Explorer 7.
The HTML Code
Create radio button like you do in regular HTML and place it any where, but do not forget to put the
name
attribute, because this solution depends on thename
attributes of the radio buttons, which is also required if you want to put your radio buttons in real action.Custom Radio Button Example Images
For the sake of simplicity, we have used separate images to specify the different states of a radio button like normal, hover, selected etc. but it is recommended to combine all these images in an image sprite form before using this solution in a real project for better performance and minimizing the HTTP request. However if you download the source code of this custom radio buttons example it will contain both the simple and sprite version. Let's take a look at the following images.
The CSS Code
What we are trying to do with the CSS is hide the default radio buttons with the help of CSS
opacity
property and show our custom radio buttons in place of them.Now place the following style rules inside the head section of your HTML document.
You might be thinking we haven't used these classes anywhere in the HTML code; actually these classes will be added to the DOM by the jQuery at run time.
The jQuery Code
Our
customRadio()
function simply wrap the default radio buttons in a<span>
element and apply the class.custom-radio
on it. Once the.custom-radio
class is applied on the<span>
elements CSS will do the magic and our custom radio button image will be displayed in place of the default radio buttons. Also, don't forget to include the jQuery in your document before this script. We've called thecustomRadio()
function when DOM is fully loaded.Tip: If JavaScript is disabled in the browser, normal radio buttons will appear instead of customized radio buttons. So it will not break your code in any way.