Jump to content

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 );
    }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.