0

Iam having a small issues with my dynamic form in javascript. when i click add supplier button, two form fields are added automatically. i can add how much fields ever i wanted. But when i click add supplier button the previously added form values are going off. What's the mistake iam doing?

 <html>
<head>
    <script type="text/javascript">
        function addTextArea(){
            var div = document.getElementById('div_quotes');
            div.innerHTML += "<input type='text' name='sup_name[]' />";
            div.innerHTML += "<input type='text' name='sup_email[]' />";
            div.innerHTML += "\n<br />";
        }
    </script>
</head>
<body>
<form method="post" action="ajax.php?tender_id=<?php echo $tender_id ?>">
<div id="div_quotes"></div>
<input type="button" value="Add Supplers" onClick="addTextArea();">
<input type="submit" name="submitted">
</form>
</body>
</html>

1 Answer 1

1

Use appendChild() instead of innerHTML that will prevent the existing form elements from getting overwritten.

function addTextArea(){
        var div = document.getElementById('div_quotes');
        var temp = document.createElement('div');
        temp.innerHTML ="<input type='text' name='sup_name[]' /><input type='text' name='sup_email[]' /><br />";
        div.appendChild(temp );
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Its not working.. When i click add supplier button, its not triggering anything...?
wait theres a mistake!
Thank you, its working now. just that a colon was missing in this line.. temp.innerHTML ="<input type='text' name='sup_name[]' /><input type='text' name='sup_email[]' /><br />". I corrected it. Thank you

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.