remove siblings after current

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theS70RM
    New Member
    • Jul 2007
    • 107

    remove siblings after current

    Hi Guys,

    Ive got a menu that dynamically creates <select> drop down boxes for categories and sub categories etc.

    When a dropdown is changed I want to remove all susequent siblings (all <select>'s) from the current div.

    Heres the code i have....

    Code:
    		
    function removeSiblingsAfterl(dd){
    
    	//remove siblings after dd
    
    	divCatsel = document.getElementById('catselect');
    	siblingNode = dd.nextSibling;
    	
    	while (siblingNode.nextSibling) {
    		divCatsel.removeChild(siblingNode);
    		siblingNode = siblingNode.nextSibling;	
    	}
    
    	//add new select box in place....
    
    }
    "dd" is the last used select box, using onchange="remov eSiblingsAfter( this)"

    So basically you will select an option from one DD box and all the ones after will be removed and then replaced by a new one.

    I just cant seem to get it removing all the sibling nodes after the current one correctly..

    Can anyone help?

    thanks

    Andy
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    [QUOTE=theS70RM]
    Code:
    		
    	while (siblingNode.nextSibling) {
    		divCatsel.removeChild(siblingNode);
    		siblingNode = siblingNode.nextSibling;
    If you've just removed siblingNode, how can you get its next sibling?
    Try this:
    Code:
    function removeSiblingsAfterl(dd)
    {
      var ns;
       while (ns=dd.nextSibling) 
    	 dd.parentNode.removeChild(ns);    
    }

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by Logician
      Try this:
      Code:
      function removeSiblingsAfterl(dd)
      {
        var ns;
         while (ns=dd.nextSibling) 
      	 dd.parentNode.removeChild(ns);    
      }
      Too right, but may give a small warning: Mistyped == as =


      [CODE=javascript]
      function removeSiblingsA fterl(dd){
      while (dd.nextSibling )
      dd.parentNode.r emoveChild(dd.n extSibling);

      }
      [/CODE]

      Comment

      • Logician
        New Member
        • Feb 2007
        • 210

        #4
        Originally posted by hsriat
        Too right, but may give a small warning: Mistyped == as =
        No - assignment was intended.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by Logician
          No - assignment was intended.
          Sure it was needed.. I'm not saying it wasn't.

          Comment

          • mrhoo
            Contributor
            • Jun 2006
            • 428

            #6
            To avoid the warning you can write assignment in conditionals like-

            while ((ns=dd.nextSib ling)!=null){

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by mrhoo
              To avoid the warning you can write assignment in conditionals like-

              while ((ns=dd.nextSib ling)!=null){
              That's a good solution.

              Comment

              • theS70RM
                New Member
                • Jul 2007
                • 107

                #8
                Originally posted by hsriat
                That's a good solution.
                thanks chaps, ill give that a try


                Andy

                Comment

                • theS70RM
                  New Member
                  • Jul 2007
                  • 107

                  #9
                  Worked a treat, thanks very much!

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Originally posted by theS70RM
                    Worked a treat, thanks very much!
                    That's fine :)

                    Glad to know you got it working.

                    Comment

                    Working...