tags:

views:

76

answers:

2

Hi everyone.

I'm unable to correctly create DOM elements using jQuery (1.3.2):

var ev_t;
$("#add-event").click(function() {
    ev_t = $("<form>", { action : "someURL" }).hide().appendTo(document.body);
    var fields = $("<div>").appendTo(ev_t);
    fields.load("someURL", function() {
        $("<input>", { type: "submit", value: "Add" }).appendTo(ev_t);
        ev_t.dialog();
    });
    ev_t.submit(function() {
        // form submission ...
        return false;
    });
    return false;
});

The and elements are inserted but none of the attributes (form action and input type and value) are set. What am I doing wrong?

+1  A: 

It seems this functionality is only available from jQuery 1.4:

As of jQuery 1.4, we can pass a map of properties to the second argument. This argument accepts a superset of properties that can be passed to the .attr() method. Furthermore, any event type can be passed in, and the following jQuery methods can be called: val, css, html, text, data, width, height, or offset.

Upgrade your jQuery libraries and you should be fine.

Aron Rotteveel
I had a couple of jQuery UI 1.7 things break with 1.4. You might want to upgrade jQuery UI to 1.8 at the same time or wait until they both stabilize.
tvanfosson
I missed that line while reading the documentation (what an idiot). Upgraded to 1.4 and ui 1.8: now it works. Thanks
Utaal
+1  A: 

The second argument to jQuery (1.3+), when there is one, is the context for the the selector, not a set of attributes to apply. Try:

ev_t = $('<form>').attr('action','someUrl").hide().appendTo(document.body);

and

$("<input>").attr({ type: "submit", value: "Add" }).appendTo(ev_t);

Or upgrade to jQuery 1.4+

tvanfosson
I solved by upgrading to 1.4.1 but I'm going to accept this answer as it explains how to do that in 1.3.*. It can be useful to people who are unable to upgrade.
Utaal