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

  • Home
  • SEARCH
  • 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 7740169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:42:29+00:00 2026-06-01T08:42:29+00:00

I cannot figure out why appendChild is returning undefined. I also tried t.previousSibling.appendChild(document.createElement(‘span’)); which

  • 0

I cannot figure out why appendChild is returning undefined.

I also tried t.previousSibling.appendChild(document.createElement('span')); which returns “previousSibling undefined”.

I need to add a span element with class of “error” to the span, withing the textnode of the label. So I loop through all the inputs and look for one that is empty, if it’s empty, it should add “*required” message next to the input label. I’ve got lots of ideas, but not sure where the problem lies. THANKS for your help!

var obj = {

inputs: document.getElementsByTagName("input"),
btn: document.getElementById('submit'),

init : function() {

    obj.btn.onclick = obj.submitForm();

    },

submitForm : function() {


    for (var i=0; i<obj.inputs.length; i++) {

        if (obj.inputs[i].value==="") {

        switch(obj.inputs[i].name) {

            case "fname" : match=true; obj.error("fname");
            case "lname" : match=true; obj.error("lname");

            }   
        }
    }
},


error : function(t) {

        var err = "*required";
        var j = t.previousSibling.appendChild(document.createElement('span'));
        j.className = "error";
        j.appendChild(document.createTextNode(err));

    }   
}


Here is HTML: 

<head>
<title>Homework 9 JavaScript form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="post" action="">
<div>
<ul>
<li><label>First Name</label><input type="text" id="fname" name="fname" size="30"  /></li>
<li><label>Last Name</label><input type="text" name="lname" size="30"  /></li>
<li><label>Phone</label><input type="text" name="phone" size="30"  /></li>
<li><label>Email</label><input type="text" name="email" size="30" /></li>
<li><input type="submit" name="submit" id="submit" value="Submit" />
</ul>
</div>
</form>
<script type="text/javascript" src="hw9.js"></script>
<script type="text/javascript">obj.init();</script>
</body>
</html>
  • 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-01T08:42:32+00:00Added an answer on June 1, 2026 at 8:42 am

    The error method appears to expect a DOM object as an argument, but you’re passing it strings. At least, the strings probably don’t have a previousSibling property that would have an appendChild method.

    You can either pass along the objects you’re already checking against:

    switch(obj.inputs[i].name) {
        case "fname" : // fall-through
        case "lname" : match=true; obj.error(obj.inputs[i]); break;
    }
    

    Or, you can change error to expect a string. Either an id — if you give <input name="lname"> an id="lname":

    error : function(id) {
        var t = document.getElementById(id);
    
        // ...
    }
    

    Or by a name:

    error : function(name) {
        var t = document.getElementsByName(name)[0];
    
        // ...
    }
    

    Also, be sure to break each case that you don’t want to fall-through into subsequent cases:

    case "fname" : match=true; obj.error("fname"); break;
    case "lname" : match=true; obj.error("lname"); break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot figure out why the following code which simply should generate random bytes
I cannot figure out a relative path that works for my resource. I've tried
I cannot figure out how to merge the following separate pieces of code: $(document).ready(function
Cannot figure out, where to change EOF in PyCharm. My scripts start with :
I cannot figure out how to write the following query using the DbFinderPlugin 1.2.2
I cannot figure out why I get this error during check-in. I checked in
I cannot figure out how to convert this code from C# to VB.net. It
I cannot figure out what my object instance names are because of how I
I cannot figure out what is obscuring my buttons. The first image shows the
I cannot figure out how to get rid of errors that should not be

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.