0

I have 5 input box in my page. I want to check if any field is blank, i will show the error message using a span tag appending to that input field.

Here is my code:

function validateForm() {   
// Declare all the local variable
var inputElements, inputId, inputType, i, inputLength, inputNode;

// Get all the input tags
inputElements = document.getElementsByTagName("input"); 

for(i = 0, inputLength = inputElements.length; i < inputLength; i++) {
    inputId = inputElements[i].id; // Get the input field ID
    inputType = inputElements[i].type; // Get the input field type

    // We will ONLY look for input[type=text]
    if(inputType === "text") {
        inputNode = document.getElementById(inputId);
        if(inputNode.value === "") {
            var spanTag = document.createElement("span"); 
            spanTag.innerHTML = inputFieldBlankErrorMessage;
            console.log(inputNode.appendChild(spanTag));
        }
    }       
}
return false; // Do Nothing

}

This is what i am getting enter image description here

It should append after the input tag. I am getting a weird tag which i don't need. Please help!!!

4
  • "is not working" is not an error description; please set up a test case or describe what is not working after debugging the JS using your browsers tools (e.g. the console output) Commented May 3, 2013 at 18:49
  • FYI, you don't need to get the .id of the element just to turn around and fetch it by its id. Just use the element from the collection. Commented May 3, 2013 at 18:49
  • Is <input><span/></input> even valid? Commented May 3, 2013 at 18:54
  • I have attached the screenshot of what i am getting...I am expecting <input /><span>some text here<span> Commented May 3, 2013 at 18:55

3 Answers 3

2

You can't .appendChild() anything to an input node, since an input can have no descendants.

Instead, you should insert the new node after it, or something similar.

inputNode.parentNode.insertBefore(spanTag, inputNode.nextSibling);

DEMO: http://jsfiddle.net/hMBHT/

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

7 Comments

Actually he wants an insertAfter. As it doesn't exists natively you should create one (using parentNode.appendChild() if last element) or just send him the jQuery code if he have jQuery on the page.
@DiegoNunes: This will do an insert after. I originally excluded the .nextSibling by mistake. The .insertBefore() method will behave like .appendChild() if there's no .nextSibling (in other words, if you don't pass an element as the second argument, it'll just do an .appendChild()).
there is the possibility of the input being the lastChild and there will be no nextSibling.
@DiegoNunes: Just updated my previous comment to explain. ;-)
@DeepakRanjanJena: inputNode.parentNode.removeChild(inputNode.nextSibling). Though you should really consider simply having the span elements in place when the page loads, but give them a class and style them with span.hidden { display: none; } in your CSS. Then it's just a matter of updating the text content of each one and showing/hiding them.
|
1

Simply put you are not supposed to append any elements to input elements.

What you probably want is something like this:

<div class="field">
  <input type="text" name="bla"/>
  <span class="error">This field can't be blank!</span>
</div>

So you need to insert the span before or after the input element. Here is an answer that shows you how.

Comments

0

I believe that your issue is that you are trying to append the span as a child of the input, not a sibling (which, I believe, is what you really want).

I can't to be sure without seeing your actual HTML, because I don't know how your inputs are situated in the DOM, but if they have separate parent elements, then you would replace:

inputNode.appendChild(spanTag);

. . . with

inputNode.parentNode.appendChild(spanTag);

Edit: FYI, the code that squint gave below (inputNode.parentNode.insertBefore(spanTag, inputNode.nextSibling);) would be how you could do it if all of the inputs are under the same parent element. It all depends on how the DOM structure is set up.

2 Comments

This will add the spanTag as the last element of the parent's children list. An insertAfter code is what he is looking for.
@DiegoNunes - Yeah, that's what I was trying to say . . . it will work if the each input is under it's own parent. Without the HTML to show the structure, it's hard to tell how to approach it. Just offering up a possibility, if that's how his HTML is set up.

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.