tonoslfx
tonoslfx

Reputation: 3442

add textbox dynamicaly using dropdown with jQuery

Dynamically add textbox using jquery
from the link above i want something like that, but i want to use dropdown menu.
here is my code
http://jsfiddle.net/boyee007/VyG6F/
the textbox will be added depends on the value.
if you select 3 will only show 3 textboxes, and if 2 will only show 2 textboxes and so on

Upvotes: 0

Views: 3724

Answers (2)

kgiannakakis
kgiannakakis

Reputation: 104178

Try something like this:

$("#ppl").change(function(){

    // The easiest way is of course to delete all textboxes before adding new ones
    //$("#holder").html("");

    var count = $("#holder input").length;
    var requested = parseInt($("#ppl").val(),10);

    if (requested > count) {
        for(i=count; i<requested; i++) {
        var $ctrl = $('<input/>').attr({ type: 'text', name:'text', value:'text'});        
            $("#holder").append($ctrl);
        }
    }
    else if (requested < count) {
        var x = requested - 1;
        $("#holder input:gt(" + x + ")").remove();
    }
});

See it running here.

Upvotes: 2

Vivek
Vivek

Reputation: 11028

here is your solution...check it out....

Solution

Upvotes: 0

Related Questions