Function that allows me to create new dropdowns from previous dropdown

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikeandike
    New Member
    • Oct 2008
    • 4

    Function that allows me to create new dropdowns from previous dropdown

    Hello all,

    I'm fairly new to javascript, and this is what I'm looking for:

    I'm pulling information from a database to a dropdown menu and I need to create a function so that when an item is selected, a new dropdown appears with the same dropdown minus whatever item was selected, I would like for this process to continue infinitely until a drop down is no longer selected. Any examples/ ideas are greatly appreciated.

    Thanks!
  • zaphod42
    New Member
    • Oct 2008
    • 55

    #2
    Based on what you said, it sounds like you need to look into "cloneNode" and "removeChil d". This is a neat article about dynamically copying form fields on your site with javascript:)

    QuirksMode domform

    Comment

    • mikeandike
      New Member
      • Oct 2008
      • 4

      #3
      Thanks, this is definitely what I was looking for and certainly puts me on the right track. I am having a difficult time getting the code for this example to execute as it does on the page, do you see anything at all wrong with the code he is using as an example? Or am I doing something wrong? I am also struggling to understand exactly where and how the function moreFields is called within the html.

      Comment

      • mikeandike
        New Member
        • Oct 2008
        • 4

        #4
        Ok, so I got the form to do what I hoped, which is create an infinite number of dropdowns with an input box next to each selection for entering a $ amount. Now, my final question is: Is there a way to sum the values entered into these boxes?

        Help is again appreciated!

        Thanks!

        Comment

        • zaphod42
          New Member
          • Oct 2008
          • 55

          #5
          when you submit you could call a function to do it....e.g.:

          Code:
          function submitMyForm(form){
             var concat;
             var i;
             for(i in form.elements){
                if(form.elements[i].value){
                  concat+=parseFloat(form.elements[i].value)
                }
             } 
             return concat
          }

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            hi ... just a note ... i wouldn't use a variable name that is a reserved word for a native js-method that is implemented for strings/arrays ... even when it would work while its used in this local scope here in the shown function ... but its just a rule of thumb to avoid such reserved names for variables in your code ... it could be really hard to debug in case you don't handle that with great care and something ever goes wrong later on ... so just avoiding that just avoids this potential errors that will drive you crazy when they occur - i promise that ;) ... i made a review some weeks ago because of a strange behaviour of an app - i took us 3h to find out that it was the variable named with a JS-keyword that made this problem ...

            as i said ... its just a hint to avoid a potential problem ;)

            kind regards

            Comment

            Working...