1

When i click a link, JS adds a new file tag to the page but every time it reloads all the file tags. What i need is that when i click on a link, it should append a file tag without re-loading earlier file tags.

Please suggest if any one has an idea about this.

Following is my code.

var intTextBox = 0; 
function addElement() 
{
    intTextBox = intTextBox + 1;
    var contentID = document.getElementById('content');
    contentID.innerHTML = "";
    var howManyTextBoxes = intTextBox;  
    for ( var i = 0; i < howManyTextBoxes; i++) 
    {               
        var newTBDiv = document.createElement('div');           
        newTBDiv.setAttribute('id', 'strText' + intTextBox);
        newTBDiv.innerHTML += "<input type=file name='fileUpload' size='40'/>";                             
        contentID.appendChild(newTBDiv);                        
}

and this is how i call to JS function.

    <a href="javascript:addElement();">Attach more files</a>

2 Answers 2

3

You should return false from that function, otherwise the link reloads the page:

function addElement() {
    intTextBox = intTextBox + 1;

    var contentID = document.getElementById('content');
    contentID.innerHTML = "";
    var howManyTextBoxes = intTextBox;  
    for ( var i = 0; i < howManyTextBoxes; i++) {               
    var newTBDiv = document.createElement('div');           
    newTBDiv.setAttribute('id', 'strText' + intTextBox);
    newTBDiv.innerHTML += "<input type=file name='fileUpload' size='40'/>";                             contentID.appendChild(newTBDiv);                            
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can call like this :

<a href="javascript:addElement();return false;">Attach more files</a>

do not forget the return false;

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.