Based on a post I’ve seen. I am creating a temporary array elements by clicking on a hyperlink. The only problem I have is that it can generate repeated elements, which until now have not been resolved:
var items = new Array();
$("ul.dropdown li a").click(function(e)
{
e.preventDefault();
var id = $(this).attr('href');
var name = $(this).text();
if(!$.findFromArray('id', id, items))
{
return false;
}
else
{
items.push({
"id" : id,
"name": name
});
//genate row
var newRow = $("<tr itemId=\"" + id + "\" />")
.appendTo(".form_list.left tbody")
.append("<td>" + name + "</td>")
.append("<td><a href=\"#\" class=\"deleteRow\">delete</a></td>");
$(".deleteRow", newRow).click(function(e) {
e.preventDefault();
items = $.removeFromArray('id', $(this).closest("tr").attr("itemId"), items);
$(this).closest("tr").remove();
});
}
});
I created a function “$.FindFromArray” that searches the array the insert and returns FALSE if there is to avoid inserting in the array and generate a line in the table:
$.findFromArray = function(property, value, arr)
{
$.each(arr, function(elem, index)
{
if(elem[property] === value)
{
return false;
}
});
};
But this function always returns me FALSE.
As I can avoid duplication before inserting the element? if a better way to do it … I hope I can help.
Change you method to
this returns
trueif it found the element in the array (makes it more readable this way), so you should also alter the check toremoved the
!