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 7872641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:15:57+00:00 2026-06-03T02:15:57+00:00

This is my javascript: <script type=text/javascript> var crct_answrs = new Array(<?php echo implode(‘,’, $crct_ans);

  • 0

This is my javascript:

<script type="text/javascript">
    var crct_answrs = new Array(<?php echo implode(',', $crct_ans); ?>);
    var answrs = new Array();

    function check_ans(){
        for(var i = 1; i < 11; i++){
            var tst = "ques"+i;
            var radio = document.getElementsByName(tst);
            for (var x = 0; x < radio.length; x ++) {
                if (radio[x].checked) {
                    answrs = radio.value;
                }   
            }
        }       
    }   
</script>

This is part of HTML:

<form>
            <div id="quest1" class="question">
                <div class="prblm">
                    <?php echo $questions[1]; ?></div>
                <table class="answrs">
                    <tr>
                        <td><?php echo $answrs[1][1]; ?></td>
                        <td><input name="ques1" type="radio" value="1"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][2]; ?></td>
                        <td><input name="ques1" type="radio" value="2"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][3]; ?></td>
                        <td><input name="ques1" type="radio" value="3"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][4]; ?></td>
                        <td><input name="ques1" type="radio" value="4"></td>
                    </tr>
                </table>
            </div>

I have 10 same pattern div tags with tables and the input name is change accordingly.
check_ans() runs on onclick. But all the time getElementsByName returns undefined. Why this happens. Please help me. Thanks!

  • 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-03T02:15:58+00:00Added an answer on June 3, 2026 at 2:15 am

    Your error lies in:

    if (radio[x].checked) {
        answrs = radio.value;
    }
    

    answrs is an array and needs to set values to the index of the corresponding iterator it’s on. radio.value is trying to access value from the radio array, but has left out it’s index as well. Look at my solution below for the fix:


    I’ve got a jsFiddle working.

    JavaScript:

    var answrs = [];
    
    function check_ans() {
        for (var i = 1; i <= 10; i++) {
            var radio = document.getElementsByName("ques" + i);
            for (var x = 0; x < radio.length; x++) {
                if (radio[x].checked) {
                    answrs[i-1] = radio[x].value;
                }
            }
        }
        console.log(answrs);
    }​
    

    Output:

    When selecting an answer 1, 2, 3, 4, 3, 2, 1, 2, 3, 4 for questions 1-10 the array is:

    ["1", "2", "3", "4", "3", "2", "1", "2", "3", "4"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: <script type=text/javascript> function js() { var getJs = document.getElementById(jogo); if
The code is like this: var script = document.createElement('script'); //script.type = 'text/javascript'; // do
var script = document.createElement( 'script' ); script.type = 'text/javascript'; script.src = 'http://www.mydomain.com/bleh.php'; script.id =
With this javascript I'm printing list of records: <script type=text/javascript> $(document).ready(function(){ function hide(){ $('#friend_list').fadeIn(100);
This is my jQuery code <script type=text/javascript> jQuery(document).ready(function($){ $(#'.$tag.').dcFlickr({ limit: '.$nFlickr.', style: thumb, q:
When building code like this: <script type=text/javascript src=<%=ResolveUrl(~/js/js.js)%>></script> or <input type=image src=<%=ResolveUrl(~/img/submit.png)%> /> Should
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I would like to add this code: <script type=text/javascript src=http://www.formstack.com/forms/js.php?1134414-uqmj2UXxEw-v2> </script> <noscript> <a href=http://www.formstack.com/forms/CampusEnterprises-chopped_greens_order_form__copy
Look this code: <script type = text/javascript> function mouseClick (container) { container.appendChild (document.createTextNode (Can
Below is the code for my YWA wrapper var astr_ywascript = (document.createElement(script).type = text/javascript).src

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.