2

Hey all I'm stumped on rewriting a function from jQuery to Javascript. The function I currently have written works great but the only issue is, is that it has to be written in plain Javascript. Here is the function I have written:

function checkFunc() {
    if ($('input[type="radio"]:checked').length === 0) alert("Not checked");
    else alert("Checked");
}

I need the function to work the same way it does now but I just need it in Javascript! Any help is appreciated. Thanks!

prasanth
  • 22,145
  • 4
  • 29
  • 53
Mujo M
  • 93
  • 2
  • 8

3 Answers3

2

Simply do with document.querySelectorAll

function checkFunc() {
  if (document.querySelectorAll('input[type="radio"]:checked').length === 0) alert("Not checked");
  else alert("Checked");
}

checkFunc()
<input type="radio" checked>
<input type="radio">
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • Thank you so much! This worked perfectly. I spent a lot of time for that same function written in Javascript and could not find one. I appreciate it! – Mujo M Sep 14 '17 at 05:35
  • You are welcome.@MujoM .My answer helped mark it as an answer.it's helpful for future visitors ... And Happy coding – prasanth Sep 14 '17 at 05:37
0

var radioBtn = document.getElementById('radioBtn');

function check() {
  if(radioBtn.checked) {
    alert('checked')
  } else {
    alert('Not Checked')
  }
}
<input type="radio" id="radioBtn" />

<button onclick="check()">Check For Checked</button>
Arun Redhu
  • 1,584
  • 12
  • 16
0

Try this one :-

function checkFunc() {
    if (!$("input[type='radio']:checked").val()) {
        alert("Not checked")
    } else {
        alert("Checked");
    }
}
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
sip
  • 11
  • 3
  • This works great, but I need the function in pure Javascript code, no jQuery. Thanks anyways! – Mujo M Sep 14 '17 at 05:36