Removing a child node I have just appended in Javascript
Hi I am very new to html and javascript. I am trying to validate a form.
When a user does not enter a date in the correct format I want a paragraph
to show below the input asking them to enter a valid date. I have managed
to do this by appending child. However, I haven't been able to remove the
child when a correct date is entered. I'm sure it is something really
simple but have had no luck looking online. Also I am wondering if there
is a way to only add the para if it does not already exist. Below is the
code.
function checkDate(date)
{
var result;
var expression = /[0-9]{2}\/[0-9]{2}\/[0-9]{4}/;
result = expression.test(date.value);
var para=document.createElement("p");
var node=document.createTextNode("Enter a valid Date");
para.appendChild(node);
var element=document.getElementById(date.id).parentNode;
if(!result===true)
{
element.appendChild(para);
}
else
{
element.removeChild(para);
}
}
No comments:
Post a Comment