tags:

views:

26

answers:

1

Hey All, thanks for the help in advance. I'm trying to create and remove input fields dynamically, they create just fine, but when i try to remove them it doesn't seem to find the div thats created, and removes the div above it, and hence all the inputs, instead of the just the one.

Heres my jscript:

$("#addp").live('click' ,(function() {
    var partid = $('#partlist').val();
    var partname = $("#partlist option:selected").text();
    $('<div>').appendTo('#parts');
    $('<label for="parts" class="left">' + partname + ': </label>').appendTo('#parts'); 
    $('<input type="text" name="part[' + partid + ']">').appendTo('#parts');
    $('<input type="button" class="removepart" value="X">').appendTo('#parts');
    $('</div">').appendTo('#parts');
    numparts++;
})); 

$(".removepart").live('click', (function () {
    $(this).closest('div').remove();
}));

and HTML

<p class="edit">Parts Used</p>
<label for="parts" class="blank">Search for a Part</label>
<input type="text" name="partsearch" id="searchp_box" /> 
<input type="button" id="searchp_button" name="search" value="Search">
<br/>
<label for="parts" class="blank">Parts Used</label>
<select name="parts" id="partlist">
</select>
<input type="button" name="search" id="addp" value="Add Part">
<br>
<div id="parts">
</div>

It should removes the closest div, which is created when you press the add button, but instead it removes the whole div id="parts" .

I searched around and was told to use .live, but it didn't seem to work :(

Thanks for reading :)

+1  A: 

When jquery appends (or appendTo's) it will try and create the DOM element, meaning this line:

$('<div>').appendTo('#parts');

Makes this html:

<div id="parts">
<div></div>
</div>

So everything after that line is not inside your new DIV at all, it keeps getting added afterwards. You can verify in FF/Firebug Chrome/Inspect Element IE/Developer Tools.

What i'd do is make your new part into a string, then append that string:

    $("#addp").live('click' ,(function() {
        var partid = $('#partlist').val();
        var partname = $("#partlist option:selected").text();
        var newPart = '<div> \
                       <label for="parts" class="left">' 
                       + partname + ': </label> \
                       <input type="text" name="part[' + partid + ']"> \
                       <input type="button" class="removepart" value="X"> \
                       </div>';
        $(newPart).appendTo('#parts');
        numparts++;
    })); 

$(".removepart").live('click', (function () {
    $(this).closest('div').remove();
}));
WSkid
Thank you! I'm fairly new to jscript in general so these DOM functions throw me sometimes. Happy Halloween :D
joe
+1 for correctness. But I would rather do `$('<div>').append(child1).append(child2)...append(childn).appendTo('#parts')` for the sake of readability and speed.
Chetan Sastry
@Chetan I was under the assumption multiple appends were bad for speed... can't seem to find a better link but http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly has some quick tests.
WSkid
You're right, but multiple appends are bad if you are doing on live DOM. In this case, you are doing the appends before the div is added to DOM. jQuery internally does it using a DocumentFragment and then adds the fragment to DOM thereby touching DOM only once. Your method is almost equally performant except for a very small overhead in parsing all the HTML. But it is less readable and error prone.
Chetan Sastry