0

I have a function that will append a select-box with possibility to choose birthdate. What is the best way to achieve that?

When i run that script the append inside the for-loop is added after the closing-select. Is there any logic to the append, or is it not strict? I thought that the forloop had to be finished before it continued.

function addChildOption(){
  if (counterOption < 3) {
    $("#add-child").append("<select name=\"child" + counterOption + "\" form=\"register\">");
    for(var i = year-15; i > 1900  ; i--){
          $("#add-child").append("<option class=\"choose-year\" value=" + i + ">" + i + "</option>");
    }
    $("#add-child").append("</select>");     
  }
}

The result:

<select form="register" name="child1"></select>
<option class="choose-year" value="1999">1999</option>
<option class="choose-year" value="1998">1998</option>
<option class="choose-year" value="1997">1997</option>
etc....
2
  • Mybe you should do $('#add-child select").append(... Commented Jun 21, 2014 at 16:38
  • You can only append elements, not tags. When you append the select element it has to be complete, you can't add the start tag and then the end tag. When you try to add the start tag, the browser will make a complete element out of it, so when you add the options they will end up after the select. Commented Jun 21, 2014 at 16:47

4 Answers 4

1

You could construct the select, add the options to that variable, and then append it to the actual form:

var sel = $('<select name="child' + counterOption + '" form="register" />');

for(var i = year-15; i > 1900; i--) {
    sel.append($('<option value="' + i + '" class="choose-year">' + i+ '</option>'));
}

$('#add-child').append(sel);
Sign up to request clarification or add additional context in comments.

Comments

1
function addChildOption(){
  if (counterOption < 3) {
    var html = "<select name=\"child" + counterOption + "\" form=\"register\">");
    for(var i = year-15; i > 1900  ; i--){
          html += "<option class=\"choose-year\" value=" + i + ">" + i + "</option>");
    }
    html += "</select>"
    $("#add-child").append(html);     
  }
}

Comments

1

For my part, I prefer to do thing like this :

function addChildOption() {
    if (counterOption < 3) {
        var addChild = $("#add-child");
        var mySelect = $("<select />").prop("name", "child" + counterOption).prop("form", "register").appendTo(addChild);
        for (var i = year - 15; i > 1900; i--) {
            $("<option />").addClass("choose-year").prop("value", i).text(i).appendTo(mySelect);
        }
    }
}

Like that, I directly add the select to the container, and then, the options to the select.

Comments

1

Use jQuery Queue

function addChildOption(){
   if (counterOption < 3)
   {
      $("#add-child").append("<select name=\"child" + counterOption + "\" form=\"register\"></select>").queue(function(next){
         for(var i = year-15; i > 1900  ; i--) {
           $(this).children().append("<option class=\"choose-year\" value=" + i + ">" + i + " </option>");
         }
      next();
     });
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.