1

I have a input type number. Once the user enters the number in that field. I want to generate that many input types under it. How can I do it with javascript or Ajax for that matter.

<table width="100%">
    No of Plans:
    <input type="text" name="numbers" value="" onkeypress="return isNumberKey(event)"/>  //using AUI here to check if numbers only
    //If the number is 5 say. I need to generate 5 input fields under here.

My Javascript which will check that the user enters only numbers is

<script type="text/javascript">
    function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }
</script>

All this is working for me. I just need the loop which would generate input fields as the value entered in numbers.

3 Answers 3

2

Something like this should help:

function createInputs() {
    var num = 123, // <-- user-defined
        i,
        input,
        body = document.getElementsByTagName("body")[0];

    for (i = 0, i < num; i++) {
        input = document.createElement("input");
        input.type = "text";

        // Add to the DOM
        body.appendChild(input);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

how would I call this function you suggested on submit. This is my submit here <td><input type="submit" value="Create" name="button" style="width: 80px;" class="rightButtons" ></td> `
Simply add: onsubmit="createInputs();" to your input element. If you want to add the new inputs at a different point (i.e. not at the end of the body) just change the name and value of the body variable in my example.
1

you can use create element to create input elements dynamically

refer this http://viralpatel.net/blogs/2009/01/dynamic-add-textbox-input-button-radio-element-html-javascript.html

function addElement(eletype){
    var ele = document.createElement("input");
    ele.setAttribute("type", eletype);
    //id is the id1 of the div or any component in which you want to add this element
    var baseele = document.getElementById("id1");
    baseele.appendChild(ele);
}

Comments

0

You find good help here

http://viralpatel.net/blogs/2009/01/dynamic-add-textbox-input-button-radio-element-html-javascript.html

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.