Reputation: 303
Hi i have a from with few radio buttons to filter my data...I want to check url to make a radio button checked...How i can achieve that ??
Form:
<form method="post">
<div>
<input id="radio2" type="radio" name="scat" value="ABC" data-href="example.com?link=[value]">
<label for="radio2">ABC</label>
</div>
</form>
Jquery:
$('input[type=radio]').click(function()
{
window.location=$(this).attr('data-href')
});
Its working fine for me all i want is to check parameters values in url and check that radio button...
Thanks...
Upvotes: 1
Views: 680
Reputation: 3294
So, are you asking to check the appropriate radio button on page load, when there is a link value in the query string of the URL?
If so, you can do that in two steps: 1. parse the URL. 2. update the correct radio button.
I'm assuming in your example, that you meant to replace [value]
with an example like ABC
to match the rest of your example. In other words <input id="radio2" type="radio" name="scat" value="ABC" data-href="example.com?link=ABC">
.
If so, then your javascript should look like this:
$(document).ready(function() {
$('input[type=radio]').click(function() {
window.location=$(this).attr('data-href');
});
// parse the url:
var link = window.location.search.match(/link=(\w+)/)[1];
if (typeof link !== 'undefined') {
// update the correct radio button:
$('input[value="' + link + '"]').prop("checked", true);
}
});
Upvotes: 1