Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic addition of textbox not working

This is quite frustrating. My code was working fine until last week. I am adding multiple textboxes to my HTML page when the user changes [using change()] the value in a dropdown selection.

Here's the HTML code snippet:

<div id="files" class="control-group">
    <label class="control-label">No. of files</label>
    <div class="controls" >
        <select id="files" name="files" class="span3">
            <option value="">--Select Option--</option>
            <?php for($i=1;$i<21;$i++){ ?>
                <option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
        </select>
    </div>
</div>
<div class="control-group" id="titles">
    <label class="control-label">File Titles</label>
    <div class="controls" id="titleAdd"></div>
</div>

Here's my Javascript / jQuery:

$(document).ready(function(){
        $("#titles").hide();
    });
    var container;
    // Add & Remove textboxes 
    $("#files").change(function(){

        //Remove all textboxes
        $("#titles").show();
        $(container).empty(); 
        $(container).remove();  
        //"DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
        container = $('<div>', {class: 'controls'});
        var option = $("#files").val();
        for(i=1;i<=option;i++)
        {
            // Add a TextBox
            $(container).append('<input style="display: block;" type=text name="Description[]" class="span3 input-left-top-margins" id="Description' + i +'"' + 'placeholder="File ' + i + ' Title" />');
        }
        $('#titleAdd').after(container);   // ADD THE DIV ELEMENT IN THE RIGHT PLACE.
    });

The most irritating part is that this code was working fine a few days ago.

like image 834
Shreyas Tripathy Avatar asked Jun 11 '15 05:06

Shreyas Tripathy


2 Answers

There are two ids #files.

See a working example here: https://jsfiddle.net/1c3b63f4/ - jQuery will always return an empty string in this assignment: $("#files").val();.

like image 142
dhh Avatar answered Oct 03 '22 05:10

dhh


Put your '$("#files").change(function(){' code inside 'document.ready':-

$(document).ready(function(){
        $("#titles").hide();

    var container;
    // Add & Remove textboxes 
    $("#files").change(function(){

        //Remove all textboxes
        $("#titles").show();
        $(container).empty(); 
        $(container).remove();  
        //"DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
        container = $('<div>', {class: 'controls'});
        var option = $("#files").val();
        for(i=1;i<=option;i++)
        {
            // Add a TextBox
            $(container).append('<input style="display: block;" type=text name="Description[]" class="span3 input-left-top-margins" id="Description' + i +'"' + 'placeholder="File ' + i + ' Title" />');
        }
        $('#titleAdd').after(container);   // ADD THE DIV ELEMENT IN THE RIGHT PLACE.
    });
 });
like image 21
Domain Avatar answered Oct 03 '22 03:10

Domain