Adding a field to a form (jQuery)

Go To StackoverFlow.com

1

I'm constructing a form. No problem there. Aside of a submit button, I have another button that, when clicked, adds another field to the form. I am attempting to use javascript (jquery) to do this. Needless to say I am not having any luck. I have tried many different methods, and this is the one I gave up at:

$("#snew").click(function () {  
var container = $("#sblc").add("div");
var content = "<select> <option>Domestic</option><option>Tiger</option><option>Peeled                  Raw</option><option>Cooked Peeled Tail On<option></select><select> <option>1 lbs</option>   <option>2 lbs</option><option>3 lbs</option></select>";

                $(container).attr("class", "opt");
                $(container).append(content); 
                $(container).appendTo("#sblc"); 

            })

Here is the HTML that corresponds: http://pastie.org/3729304 (I couldn't figure out how to get the HTML to display properly on here. Sorry for the link. )

Whenever I try this, I end up either getting a completely white page, or nothing happens at all. Any help would be greatly appreciated.

2012-04-04 20:55
by Da General
Your problem might be here var container = $("#sblc").add("div"); and then you wrap container in jQuery twice, no need to do that - elclanrs 2012-04-04 20:58


0

Try something like below,

DEMO

$("#snew").click(function() {
    var container = $("<div />");
    var content = "<select> <option>Domestic</option><option>Tiger</option><option>Peeled                  Raw</option><option>Cooked Peeled Tail On<option></select><select> <option>1 lbs</option>   <option>2 lbs</option><option>3 lbs</option></select>";

    $(container)
        .attr("class", "opt") //add class to the div container
        .append(content)      //append select to the div
        .appendTo("#sblc");   //append div to #sblc

});
2012-04-04 21:03
by Selvakumar Arumugam
I see. I continually referenced the container when it only needed one reference, and by the logic of my first line, it was already appended to the initial div (which didn't work anyway). Thanks for clearing that up for me - Da General 2012-04-04 21:21


0

I think you need to do:

var container = $("<div>"); // notice the `<` and `>`

This way, you create new DIV container and append it later (you've already done this)

2012-04-04 20:59
by Jovan Perovic
I tried that, and at this point it deleted everything else in the form. It's a start. It's actually doing something.. - Da General 2012-04-04 21:01


0

What about simply calling append once:

    $("#sblc").append("<div class=\"opt\"><select><option>Domestic</option><option>Tiger</option><option>Peeled                  Raw</option><option>Cooked Peeled Tail On<option></select><select> <option>1 lbs</option><option>2 lbs</option><option>3 lbs</option></select></div>");
2012-04-04 21:00
by ckruse
Thank you, kind sir - Da General 2012-04-04 21:03


0

You have an error with how your appending.

http://jsfiddle.net/NYnYw/

2012-04-04 21:02
by mikevoermans


0

This will work with the code you provided:

$("#snew").click(function () {
    var container = $("#sblc");
    var content = "<div><select> <option>Domestic</option><option>Tiger</option><option>Peeled                  Raw</option><option>Cooked Peeled Tail On<option></select><select> <option>1 lbs</option>   <option>2 lbs</option><option>3 lbs</option></select></div>";

    $(container).attr("class", "opt");
    $(container).append(content);
})
2012-04-04 21:03
by CodeLikeBeaker
Even better. Again thank you - Da General 2012-04-04 21:07


0

Your logic seems weird... You first declare container as $("#sblc").add("div") which refers to a jQuery collection of objects that includes both #sblc and as many div as it can match. Is this really what you're trying to do?
Then, you double wrap container in jQuery object which is unnecesary.
The part that strikes me as unusual is $(container).appendTo("#sblc"). You're appending the double wrapped container, or #slbc + divs to #slbc?? Don't understand...
You could try something like this:

$("#snew").click(function () {

    var $container =  # ('#slbc'),
        materials = ['Domestic', 'Tiger', 'Peeled Raw', 'Cooked Peeled Tail On'],
        values = ['1 lbs', '2 lbs', '3 lbs'],
        html = '<select><option>' + materials.join('</option><option>') + '</option></select>' +
            '<select><option>' + values.join('</option><option>') + '</option></select>';

    $container.append(html).addClass('opt');

});
2012-04-04 21:08
by elclanrs
Ads