-1

I had developed a jsp web project, where user has to select one option from radio button, and i m sending the user selected value to other page using javascript.

JavaScript code:

function select() {
    var val1 = document.getElementsByName("President");
    var result1;
    for (var i = 0; i < val1.length; i++) {

        if (val1[i].checked) {
            console.log(val1[i].checked)
            result1 = val1[i].value;
            alert(result1);

        } else {

            alert("president vote is missing");
            return;
        }

    }
}

When the user doesn't choose an option, then javascript code is working fine and it is going to else and showing an alert message that("president vote is missing"). But when user chooses one option from the list then then the if condition is working fine and after that else condition is also getting executed , I never faced this kind of problem

2
  • 2
    console.log(val1[i].checked) should have a semi-colon - it might be useful to see the markup on this issue perhaps? Commented Aug 18, 2015 at 2:16
  • 1
    Could you show us the dom tree on jsfiddle so that we can better Understand your question. Commented Aug 18, 2015 at 2:19

2 Answers 2

2

I can see you doing alerts in a for loop. Can it be a scenario like this: first iteration "if" gets executed and then next iteration "else" gets executed because of "val1[1].checked" is false?

Sign up to request clarification or add additional context in comments.

4 Comments

It cant be like that because the loop will iterate till it find a check value, and i found the problem by your solution can you please tell me how to solve it now
I am not sure what exactly you intent to do. If "iterate till it find a check value" is what you after, why not add a break in the 'if' clause to break out of the for loop once the check value is found.
if (val1[i].checked) { console.log(val1[i].checked); result1 = val1[i].value; break; }
Thanks a lot Peter Peng I forgot about for loop now i fixed the issue now
0

Beside`s the problem is solve, i made this code if someone needs help!

<html>
<head>
    <title>STACK OVERFLOW TESTS</title>
</head>
<body>
    <input type = 'radio' name = 'President' value = 'Obama'>Obama</input>
    <br>
    <input type = 'radio' name = 'President' value = 'Bush'>Bush</input>
    <br>
    <input type = 'radio' name = 'President' value = 'Clinton'>Clinton</input>
    <br>
    <input type = 'button' id = 'button' onClick = 'showMyChoice()'> SEND </input>

    <script>

    function showMyChoice(){
        var val1 = document.getElementsByName("President");
        var result1;
        var missingVote = false;

        for(var i = 0; i < val1.length; i++){
            if(val1[i].checked){ // Check for all the radio buttons if there is one checked.
                console.log(val1[i].checked);
                result1 = val1[i].value;
                alert(result1);
                missingVote = false;
                break;
            }
            else{
                missingVote = true;
                continue;
            }
        }

        if(missingVote){
            alert('President vote is missing');
        }
    }
    </script>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.