Simple way to check radio button on window load.
Consider your url to be of form –
scheme://example.com/myproject/abc.php?id=2
After running below code snippet you will encounter an error so try it on your site it requires url inorder to work.
$(window).on('load', function() {
let prepend = 'item-';
let elem = 'input[type=radio]';
let url = window.location.href;
let keyVal="1";
if (url.indexOf('?') !== -1)
keyVal = url.split('?')[1].split('=')[1];
let id = prepend + keyVal;
$(elem).each(function(i) {
if ($(this).prop('id') === id)
$(this).prop('checked', true);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="price" id="item-1">
<input type="radio" name="price" id="item-2">
<input type="radio" name="price" id="item-3">
<input type="radio" name="price" id="item-4">
<input type="radio" name="price" id="item-5">
Hope, this works for you..!! 🙂 🙂
4
solved Select radio button from external URL [closed]