1

The following script is called upon to dynamically add html input elements. There are two issues with the marked code (about 30 lines in) below.

1) Two input boxed should be displayed, when I add the marked code below the second input box (foox1 - "last name") is not being displayed.

2) The second is that I need id='suggestions' to be dynamic i.e. linked to foox1. I should be using setAttribute as I have with the rest of the code but I need to get 1) above working first.

<script language="javascript">

var x = 1;

function add() {
var fooId = "foo";

for (i=1; i<=2; i++)
  {
    //Create an input type dynamically.
    var element = document.createElement("input");

    //Assign different attributes to the element.
    element.setAttribute("type", fooId+x+i);
    element.setAttribute("name", fooId+x+i);
    element.setAttribute("id", fooId+x+i);

    if(i==1){
           element.setAttribute("value", "First name");
           element.setAttribute("onkeyup", "lookup(this.value);");
           element.setAttribute("onblur", "fill();");

     }
    if(i==2){
          element.setAttribute("value", "Last name");

    }
    var foo = document.getElementById("fooBar");
    foo.appendChild(element);

    /************************************************
    This is where the problem is, in this if statement.  
    This part of the code is required for an autofil in the text box

    ************************************************/

    if(i==1){
        foo.appendChild("<div class='suggestionsBox' id='suggestions' style='display: 
            none;'><img src='upArrow.png' style='position: relative; top: -12px; left: 30px;'         
            alt='upArrow' /><div class='suggestionList' id='autoSuggestionsList'>&nbsp;       </div>    </div>");
    }

    /********************************
    end of code with issues
    ******************************/

    var space = document.createTextNode(" ");
    foo.appendChild(space);  

}
        i++;            
            var element = document.createElement("input");
    element.setAttribute("type", "button");
    element.setAttribute("value", "Remove");
    element.setAttribute("id", fooId+x+i);
    element.setAttribute("name", fooId+x+i);    
    element.setAttribute("onclick", "removeRow(this)");
    foo.appendChild(element);

    var br = document.createElement("br");
    foo.appendChild(br);

    x++;

}
</SCRIPT>
1
  • 2
    please add a jsfiddle example Commented Sep 1, 2013 at 6:46

2 Answers 2

1

You cannot use .appendChild in the below context. .appendChild adds a node not a string.

foo.appendChild("<div class='suggestionsBox' id='suggestions' style='display: 
            none;'><img src='upArrow.png' style='position: relative; top: -12px; left: 30px;'         
            alt='upArrow' /><div class='suggestionList' id='autoSuggestionsList'>&nbsp;       </div>    </div>");

It has to be

foo.innerHTML += "<div class='suggestionsBox' id='suggestions' style='display: none;'><img src='upArrow.png' style='position: relative; top: -12px; left: 30px;' alt='upArrow' /><div class='suggestionList' id='autoSuggestionsList'>&nbsp;  </div>   </div>";

Working Demo

EDIT: For the second part of your question, you can do id='suggestions_for_' + fooId + x + i in the above mentioned statement. (It will produce something like id='suggestions_for_foo11'.)

Sign up to request clarification or add additional context in comments.

Comments

0

you need to end each of those first two lines with " +

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.