views:

234

answers:

1

This should be my last question on Jquery Sortable...for a while :)

I have a list that I'm able to remove elements from dynamically. when users click the X close box on the element, I get the parent (the element itself) and remove it:

    function DeleteLink() {
        var jItem = $(this).parent();

        var LinkId = jItem[0].childNodes[1].innerText || jItem[0].childNodes[3].textContent;
        var LinkTitle = jItem[0].childNodes[2].innerText || jItem[0].childNodes[5].textContent;

        if (!confirm(String.format("Are you sure you want to delete link #{0}?\n\nTitle: {1}", LinkId, LinkTitle)))
            return;

        jItem.remove();
        $.post("/Home/DeleteLink/" + LinkId);
        showStatus("Link deleted...", 5000);
    }

If you are interested, unordered list is created like this:

<ul id="sortable1" class="connectedSortable">
        <% foreach (Link l in Model)
             {
                 if (l.Visible == true)
                 {%>
                    <li class="photolistitem" style="min-height:50px;">
                    <div class="imagecontainer"><img src="/Content/images/link.jpg" style="float:left; padding-right:5px;" class="thumbnailimage" alt="" /></div>
                    <div id='<%=l.Id%>Id'><%=l.Id%></div>
                    <div id='<%=l.Id%>Description'><%=l.Title%></div>
                    <div id='<%=l.Id%>Description'><%=l.Description%></div>
                    <div id='<%=l.Id%>Description'><%=l.Url%></div>
                    <div class='deletethumbnail'>X</div>
                    </li>
               <%}%>
       <%}%>  
    </ul>

What I want to do is to have a form on the bottom of the page so that a user can add an element dynamically - They will only need to insert a description, a title, and a url (I will use another jquery plugin to validate the input).

I think the biggest challenge will be dynamically creating an object that can be appended to the list. Can anyone point me in the right direction for this?

+2  A: 

jQuery

thisDependant = $('.DependantTemplate').clone();
$(thisDependant).removeClass("DependantTemplate");
$(thisDependant).addClass("Dependant" + dependantNum);
$('.DependantList').append(thisDependant);

HTML

<div class="DependantTemplate hidden">
  <div style="float:left;" class="DependantNumber spacerRight10"></div>
  <div style="float:left;" class="DependantFirstName spacerRight10"></div>
  <div style="float:left;" class="DependantLastName spacerRight10"></div>
  <div style="float:left;" class="DependantDateOfBirth spacerRight10"></div>
  <div style="float:left;" class="DependantGender spacerRight10"></div>
  <div style="float:left;" class="DependantType"></div>
  <div class="clearFloats"></div>
</div>

<div class="DependantList"></div>

The above is what I use to do the same as you're looking for.

griegs
That's perfect, and much less painful that I had imagined. Thank you!
splatto