javascript - Select radio button from external URL -


how select radio button using external link such /page.html#item1 or /page.html?item1 when page loads it's selected

<input type="radio" name="price" id="item-1"> <input type="radio" name="price" id="item-2"> <input type="radio" name="price" id="item-3"> 

any method do, javascript or jquery

simple way check radio button on window load.

consider url of form - scheme://example.com/myproject/abc.php?id=2

after running below code snippet encounter error try on site requires url inorder 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, works you..!! :) :)


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -