We help IT Professionals succeed at work.

Html value of radio

Whing Dela Cruz
on
191 Views
Last Modified: 2017-03-09
Hi experts, I want to get the value of radio by name and put it in an alert. My code below won't work.

<!DOCTYPE html>
<html>
<body>
<div>
<br>Rush Order
<input type="radio" name="kind" value="rush">
<br>Regular
<input type="radio" name="kind" value="regular">
</div>
<br>
<button onclick="Get()">Try</button>

<script>
function Get()
{
      var x = document.getElementByName("kind").value
      alert(x);
}
</script>
</body>
</html>
Comment
Watch Question

Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
try this for your JS function:

function Get()
{
      var x = document.querySelector('input[name="kind"]:checked').value;
      alert(x);
}
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
also, you may want to rename your function, I can't remember if get is a keyword in JS

Author

Commented:
Its working Sir, but what about if I put a condition like this, can you correct this before I close? Thanks

If ( document.querySelector('input[name="kind"]:checked').value = "")
{
return;
}
else
{
 alert("Correct!")
}
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
code looks ok, what happens when it runs? what do you expect to happen?

Author

Commented:
I want to make something like this,
      var x = document.querySelector('input[name="kind"]:checked').value;    
     If ( x = "")
      {
        alert("No")
      }
      {
        alert(x)
      }

Author

Commented:
Hi Monty, My purpose is that I want an alert message "No" once either of the two are not choosen. Thanks!
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
then try this:

function Get()
{
      var opt= document.querySelector('input[name="kind"]:checked');     //-- this'll return a true or false value
      if( opt ) {
          alert( document.querySelector('input[name="kind"]:checked').value);
      else
           alert('no');
}

Author

Commented:
Its not working sir, and no error appear.
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
please post your entire code

Author

Commented:
Here it is..

<!DOCTYPE html>
<html>
<body>
<div>
<br>Rush Order
<input type="radio" name="kind" value="rush">
<br>Regular
<input type="radio" name="kind" value="regular">
</div>
<br>
<button onclick="Get()">Try</button>

<script>
function Get()
{
      var opt= document.querySelector('input[name="kind"]:checked');     //-- this'll return a true or false value
      if (opt)
      {
          alert( document.querySelector('input[name="kind"]:checked').value);
      else
           alert("no");
      }
}
</script>
</body>
</html>
Web Ninja at large
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Its working also to my project. Thanks you!
Big MontyWeb Ninja at large
CERTIFIED EXPERT

Commented:
my pleasure

Author

Commented:
More power and God Bless you sir!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.