Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9273869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:12:55+00:00 2026-06-18T16:12:55+00:00

I am looking, in the cleanest way possible to be able to take a

  • 0

I am looking, in the cleanest way possible to be able to take a simple yes or no question and test for which answer has been chosen.

For instance in this case if a “yes” I want it to return a value of “Continue” into a specified area (a 3 column table which has a question in the first, the radio buttons in the second, and I want it to update and display the answer in the third).

My code for the JS stands thus far:

<script type="text/javascript">

var answer = 'place-holder';

var user_input;


function checkForm() 
{
    var substanceab = document.getElementById('Sub');
    for (i = 0; i < substanceab.length; i++)
    {
        if(substanceab.length[i].checked)
        {
            user_input = substanceab.length[i].value;
        }
    }

    if (user_input ="yes")
    {
        return answer = "Continue";
    }
    else if (user_input ="no")
    {
        return answer = "Provide Alternate Referral";
    }
    else
    {
        return;
    }   
};
function writeAns()
{
    document.getElementById('answerwrite').innerHTML = checkForm[user_input];
};

</script>

and the Body text (minus the actual question):

<table class="table table-bordered">
 <tr>
  <td width="58%"><center><strong>Required:</strong></center></td>
  <td width="19%"></td>
  <td width="23%"><center><u><strong>___________</strong></u></center></td>
  </tr>
  <tr>
   <td> <span class="sub depend"> <strong><i>_________</i> </strong></span></td>
   <td> <span class="sub depend ans">
     <form name="Substance">
      <label class="radio inline">
        <input type="radio" value="yes" name="substance" onclick="writeAns()">
          yes </label>
      <label class="radio inline">
        <input type="radio" value="no" name="substance" onclick="writeAns()">
          no </label> </form></span></td>
          <div id="answerwrite"></div>
      <script type="text/javascript">
document.write("<td>" + writeAns() + "</td>")
</script>

</tr></table>

Ok, fixed the ‘funk’ but like I said, more used to java than javascript, completely tougher syntax. I agree, I only used the ID thing to try and get this to work with a different method, but it never did. Now with some of the suggestions it is just giving me undefined everywhere. And while I agree this will eventually turn to jquery, I have NO clue how to work with it, hence figuring out this in javascript first.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:12:56+00:00Added an answer on June 18, 2026 at 4:12 pm

    A few things:

    document.getElementByID() 
    

    will always return the first element, as ID’s are supposed to be unique.
    Instead, use:

    document.getElementsByName('substance');
    

    Next, in your loop, you are improperly referencing the array’s members using the .length property. Try this instead:

        for (i = 0; i < substanceab.length; i++)
        {
            if(substanceab[i].checked)
            {
                user_input = substanceab[i].value;
            }
        }
    

    Finally, in javascript, the ‘=’ opertator is always an assignment statement. To do comparisons, always use ‘==’:

        if (user_input == "yes")
        {
            return answer = "Continue";
        }
        else if (user_input =="no")
        {
            return answer = "Provide Alternate Referral";
        }
        else
        {
            return;
        }  
    

    Also, try to avoid global variables whenever possible.
    Here is the working code, refactored a bit:

    <input type="radio" value="no" id= "Sub" name="substance" onclick="writeAns()">
    
    function checkForm() 
    {
        var user_input;
    
        var substanceab = document.getElementsByName('substance');
        for (i = 0; i < substanceab.length; i++)
        {
            if(substanceab[i].checked)
            {
                user_input = substanceab[i].value;
            }
        }
    
        if (user_input == "yes")
        {
            return "Continue";
        }
        else if (user_input =="no")
        {
            return "Provide Alternate Referral";
        }
        else
        {
            return;
        }   
    };
    function writeAns()
    {
        document.getElementById('answerwrite').innerHTML = checkForm();
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for suggestions on cleanest way to get the return value and the result
I was looking for the best/cleanest way to iterate over a list of strings
I’m looking for the easiest, cleanest way to add X months to a JavaScript
I'm looking for the simplest and cleanest way to render a basic DateField with
I am looking to determine the cleanest / best practice way to complete the
I am looking for the cleanest way to see that a datagridview is in
When using the Ti.UI.iPad.SplitWindow what is the best(cleanest looking) way to update the detailView
I'm looking for the cleanest way to get all open windows and have access
Looking for a way to enable someone to upload a single file which will
I'm looking for the cleanest way to have a countdown timer fire when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.