removing a node.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bmgz

    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(v arForm){
    var varReturn = true;
    for(var i=0; i<varForm.lengt h; i++){
    if(varForm.elem ents[i].className.inde xOf('required') != -1 &&\
    varForm.element s[i].value == ""){
    varSpan=documen t.createElement ('span');
    varMsg=document .createTextNode ('* This field is required!');
    varSpan.appendC hild(varMsg);
    varSpan.setAttr ibute("class", "error");
    varForm.element s[i].parentNode.app endChild(varSpa n);
    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?
  • Martin Honnen

    #2
    Re: removing a node.



    bmgz wrote:
    [color=blue]
    > 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(v arForm){
    > var varReturn = true;[/color]

    I don't think it makes sense to prefix any variable name with var,
    instead that makes reading the code harder.
    [color=blue]
    > for(var i=0; i<varForm.lengt h; i++){
    > if(varForm.elem ents[i].className.inde xOf('required') != -1 &&\
    > varForm.element s[i].value == ""){
    > varSpan=documen t.createElement ('span');[/color]

    You need to somewhere store those elements you create, either use an
    array that is associated with the form e.g.
    varForm.validat ionSpans = [];
    and add those spans as needed e.g.
    varForm.validat ionSpans[varForm.validat ionSpans.length] = varSpan;
    or associate each span with the element e.g.
    varForm.element s[i].validationSpan = varSpan;
    The later on you can remove those spans, removing an element is as easy as
    element.parentN ode.removeChild (element);
    [color=blue]
    > varMsg=document .createTextNode ('* This field is required!');
    > varSpan.appendC hild(varMsg);
    > varSpan.setAttr ibute("class", "error");[/color]

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


    --

    Martin Honnen

    Comment

    • bmgz

      #3
      Re: removing a node.

      Martin Honnen wrote:[color=blue]
      > I don't think it makes sense to prefix any variable name with var,
      > instead that makes reading the code harder.[/color]

      I am used to $variablename, trying to establish some new bad habits as I
      am new to JS ;-)
      [color=blue]
      > You need to somewhere store those elements you create, either use an
      > array that is associated with the form e.g.
      > varForm.validat ionSpans = [];
      > and add those spans as needed e.g.
      > varForm.validat ionSpans[varForm.validat ionSpans.length] = varSpan;
      > or associate each span with the element e.g.
      > varForm.element s[i].validationSpan = varSpan;
      > The later on you can remove those spans, removing an element is as easy as
      > element.parentN ode.removeChild (element);
      >[/color]

      dynamite, thanks.
      [color=blue]
      > varSpan.classNa me = "error"[/color]

      thanks for that, I think it was like that, then I changed it for no
      particular reason..

      Comment

      Working...