Jump to content

dynamically adding form input fields


dadamssg87

Recommended Posts

I'm trying to write an onclick function that appends a div with input fields in it to an existing div...if that makes sense. I suck at javascript though. I can't even append the div with one input field in it though.

 

anybody any good with javascript?

 

http://jsfiddle.net/XLuHU/1/

Link to comment
https://forums.phpfreaks.com/topic/248364-dynamically-adding-form-input-fields/
Share on other sites

got it

 

function add_item( code, title, price, divbox )
{
    var idtag, item_title, item_price, item_code, separator;
    
    separator = "     ";
    
    // Generate an id based on timestamp
    idtag = "div_" + new Date().getTime();
    // Generate a new div.
    $( divbox ).append( "<div id=\"" + idtag + "\"></div>" );

    if ( divbox == "#item_box1" )
    {
        item_title = $("<input/>", {
            type: 'text',
            name: "box1_item_title[]",
            value: title
        });

        item_price = $("<input/>", {
            type: 'text',
            name: "box1_item_price[]",
            value: price
        });
        
        // Show in the document.
        $( "#" + idtag ).append( item_title, separator, item_price );
    }
    else
    {
        item_code = $("<input/>", {
            type: 'text',
            name: "box2_item_code[]",
            value: code
        });

        item_title = $("<input/>", {
            type: 'text',
            name: "box2_item_title[]",
            value: title
        });
        // Show in the document.
        $( "#" + idtag ).append( item_code, separator, item_title );
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.