473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

removing a node.

I have written a simple function that validates a form based on the form
objects' className attribute. The form basically write a "field
required" message next to the form element that is blank(and is
required). I have implemented all the Regex stuff onload already.

function checkRequired(varForm){
var varReturn = true;
for(var i=0; i<varForm.length; i++){
if(varForm.elements[i].className.indexOf('required') != -1 &&\
varForm.elements[i].value == ""){
varSpan=document.createElement('span');
varMsg=document.createTextNode('* This field is required!');
varSpan.appendChild(varMsg);
varSpan.setAttribute("class", "error");
varForm.elements[i].parentNode.appendChild(varSpan);
varReturn = false;
}
}
return varReturn;
}

I am having difficulty removing the new nodes I have created. Each time
I click submit, I want the nodes I created to be removed, otherwise I
just keep on appending the same message string each time. Any suggestions?
Jul 23 '05 #1
2 1517


bmgz wrote:
I have written a simple function that validates a form based on the form
objects' className attribute. The form basically write a "field
required" message next to the form element that is blank(and is
required). I have implemented all the Regex stuff onload already.

function checkRequired(varForm){
var varReturn = true;
I don't think it makes sense to prefix any variable name with var,
instead that makes reading the code harder.
for(var i=0; i<varForm.length; i++){
if(varForm.elements[i].className.indexOf('required') != -1 &&\
varForm.elements[i].value == ""){
varSpan=document.createElement('span');
You need to somewhere store those elements you create, either use an
array that is associated with the form e.g.
varForm.validationSpans = [];
and add those spans as needed e.g.
varForm.validationSpans[varForm.validationSpans.length] = varSpan;
or associate each span with the element e.g.
varForm.elements[i].validationSpan = varSpan;
The later on you can remove those spans, removing an element is as easy as
element.parentNode.removeChild(element);
varMsg=document.createTextNode('* This field is required!');
varSpan.appendChild(varMsg);
varSpan.setAttribute("class", "error");


Doing
varSpan.className = "error"
is the preferred way as it works with both IE and other browsers while
using setAttribute with IE would require setAttribute("className", "error")
to work.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin Honnen wrote:
I don't think it makes sense to prefix any variable name with var,
instead that makes reading the code harder.
I am used to $variablename, trying to establish some new bad habits as I
am new to JS ;-)
You need to somewhere store those elements you create, either use an
array that is associated with the form e.g.
varForm.validationSpans = [];
and add those spans as needed e.g.
varForm.validationSpans[varForm.validationSpans.length] = varSpan;
or associate each span with the element e.g.
varForm.elements[i].validationSpan = varSpan;
The later on you can remove those spans, removing an element is as easy as
element.parentNode.removeChild(element);

dynamite, thanks.
varSpan.className = "error"


thanks for that, I think it was like that, then I changed it for no
particular reason..
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jimmy | last post by:
Hi everyone, I am working with a binary tree, and I am having a bit of trouble visuallizing what needs to happen when I am trying to delete a node that has two children. (no child node and one...
4
by: Grant Wagner | last post by:
There doesn't seem to be any mechanism to "clear" a node of all it's children (not that its necessary very often, but I have come across situations where I'd like to clear a node of all it's...
2
by: Raja Kannan | last post by:
Is there a way to remove text portion from the HTML keeping the HTML Tags using the browser, say javascript RegEx or something ? I have seen lot of examples removing HTML tags to get the text...
3
by: e-mid | last post by:
Here is an xml structure. i want to remove <a> nodes that do not have any child. How can i do that in csharp? <root> <a> <b/> </a> <a/> <a/> <a> <c/>
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
7
by: Simon Hart | last post by:
Hi, I have a requirement to remove the xmlns from the DOM in order to pass over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if there is a xmlns present!?! The reason I have a...
0
by: jonny | last post by:
Hi, I have a template "delete-node" with two arguments containing document fragments when called. I want to return a new document fragment which contains all nodes from argument 2 except the...
5
WebDunce
by: WebDunce | last post by:
Hi guys, I am trying to develop a simple xml editor. I have an object that has a TreeView. I use the TreeView to display the Xml node info. That's all fine, but i want the user to be able to move...
0
by: jambalapamba | last post by:
Hi I am removing node from a document depending on the style property and this is taking 7 seconds for checking complete document is there any ideas how can i improve the perfomance. Here is...
2
by: MedIt | last post by:
Hi, I am new in using xsl.I am stuck in a simple task,which is trying to remove an atribute from a node,and keep the rest of the attributes of this element in the xslt. I have tried a couple of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.