Jump to content

JavaScript createTextNode


Go to solution Solved by denno020,

Recommended Posts

I'm using JavaScript to validate a form field. Basically, if the form is submitted and the default value of the field is not altered, a text node with an error message will be generated. Problem is if the submit button is clicked over and over, the error message will appear just as many times as the button is clicked,

 

ie. Incorrect information.Incorrect information.Incorrect information.Incorrect information.

 

How do I make the generated message appear only on the first button submit. I tried this but it

 

JavaScript:

window.onload = function(){
	'use strict';
	
	var errorMsg = 'Incorrect information.';
	
	document.getElementById('btn').onclick = function(){
	
		if(document.getElementById('btn').value == document.getElementById('btn').defaultValue){
			var msg = document.createTextNode(msg);
			document.getElementById('btn').parentNode.appendChild(msg);
			/* This if statement didn't work
			if(msg){
				document.getElementById('btn').parentNode.replaceChild(msg);
			}*/
		}
	};
	
};

html:

<body>
<form action="#" method="POST" id="theForm">
<div><input type="text" name="firstName" id="firstName" value="First Name"/></div>
<input type="button" name="btn" id="btn" value="Submit Info"/>
</form>
<script src="js/validate.js"></script>
</body>
</html>
Edited by spence911
Link to comment
https://forums.phpfreaks.com/topic/287938-javascript-createtextnode/
Share on other sites

Your code doesn't seem to work?

 

Anyway, one way you could do it is to add your error message to a container of some sort, either a div or span, and then whenever the form is validated, the first thing you do is clear the content of that container.

 

Then the validator will run as per normal, adding the error message to the container again if the form still doesn't validate.

 

Something like

<form>
  <div id="error-message"></div>
  <input type="text" id="the-input"/>
  <input type="submit" id="the-submit-btn"/>
</form>

<script>
document.getElementById('the-submit-btn').onclick = function(){
  document.getElementById("error-message").innerHTML = "";
 //Perform your validation, adding the error message back to the container, if required
}
</script>

That's obviously some really rough code, but hopefully it will give you the idea of what I'm suggesting.

Edited by denno020

Thanks denno020. I created a <p> container with the id of output and added this at the top of the function: document.getElementById('output').innerHtml = '';. But I'm still getting an error messages on every click instead of just one.

 

html:

<body>
<div><label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" value="First Name"/></div>
<input type="button" name="btn" id="btn" value="Submit"/>
<p id="output"></p>
<script src="js/validayt.js"></script>
</body>
</html>

js:

window.onload = function(){
	'use strict';
	
	document.getElementById('btn').onclick = function(){
		document.getElementById('output').innerHtml = '';
		if(document.getElementById('firstName').value == document.getElementById('firstName').defaultValue){
			var msg = document.createTextNode('Incorrect information.');
			document.getElementById('output').appendChild(msg);
		}
		
	};
	
};
  • Solution

The problem is because you're creating a new element, and then appending it.

 

If you change it to use innerHtml for displaying the error message, then it will only show up once.

 

 

if(document.getElementById('firstName').value == document.getElementById('firstName').defaultValue){
    document.getElementById('output').innerHtml = "Incorrect information.";
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.