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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:16:31+00:00 2026-06-15T18:16:31+00:00

below I used a for loop to loop through each question: for (var i=0;i<=questions;i++){

  • 0

below I used a for loop to loop through each question:

for (var i=0;i<=questions;i++){   
    if ($("[class*=q" + i + "_mark]").length == 1) {
        var bold_marks='<strong>0</strong>';
        var t_marksHtml = t_marks.val();
        $("[class*=q" + i + "_mark]").val(t_marksHtml).attr('readonly', true);
    }                    
}

Now what I want to do is create a validation function so that it validates through each question and each elements within each question, but the problem is that I am trying to use the foreach function to loop through each row like so:

function validation() {

    var alertValidation = "";
    var _qid = "";
    var _msg = "";
    var marks = parseInt($("[class*=q" + i + "_mark]").text()); 

    _qid = $(".q"+questionno+"_ans_org").text();
    _msg = "You have errors on Question Number: " + _qid + "\n";

$("[class*=q" + i + "_mark]").each(function() {

if (!this.value) {
    alertValidation += "\n\u2022 You have not entered in a value in the Indivdiaul Marks textbox all your Answers\n";
}

if (alertValidation != "") {
    return false; //Stop the each loop 
}
});    

     $(".q"+questionno+"_ans_org").each(function(){


if($("[class*=q" + i + "_mark]").text() < '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) +  " Marks";   
}

        else if($("[class*=q" + i + "_mark]").text() > '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks +  " Marks Remaining";   
}

            if (alertValidation != "") {
                return false; //Stop the each loop 
            }

});

    if (alertValidation != "") {
        alert(_msg + alertValidation);
        return false;
    }

    return true;
}

Obviously because I am using the foreach method, the i is undefined. Now what my question is that first of all is it better if I use the foreach loop or the for loop for the validation function above and does anyone know how to fix the code above so that the code is working so that I don’t get an i undefined error?

Below is the html code:

<table border='1' id='markstbl'>
<thead>
<tr>
<th class='answermarksth'>Marks per Answer</th>
<th class='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>

<tr class="questiontd">
<td class="answermarkstd">
<input class="individualMarks q1_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<td class="totalmarkstd" rowspan="2">6</td>
<td class="noofmarkstd q1_ans_text"  q_group="1" rowspan="2"><strong>5</strong></td>
</tr>

<tr class="questiontd">
    <td class="answertd" name="answers[]">D</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
</tr>

<tr class="questiontd">
<td class="answermarkstd">
<input class="individualMarks q2_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<td class="totalmarkstd" rowspan="1">6</td>
<td class="noofmarkstd q2_ans_text"  q_group="1" rowspan="1"><strong>5</strong></td>
</tr>
</tbody>
</table>
  • 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-15T18:16:33+00:00Added an answer on June 15, 2026 at 6:16 pm

    Instead of

    $("[class*=q" + i + "_mark]").each(function() {
    

    Just use

    $("[class*='q'").each(function(i) {
    

    The i in the $.each loop will be the current input class for the iteration..

    Follow the same approach for the other loops as well..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a function (below) which is used in my mysql abstraction class and
I have a for loop running through 500.000ish list. For each of these it
I have 3 divs within a div container, I loop through each of them.
A query that is used to loop through 17 millions records to remove duplicates
Code below is used to save PostgreSql database backup from browser in Apache Mono
I have used below code to attach quckboook events // Subscribe to UI events...
The markup below is used for my jQuery carousel widget. The first div serves
I am having a very specific problem in JQuery The code below is used
I want to Display Google Map into my wordpress site. I have used below
I used the below jcl to modify the existing ps. I was able to

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.