I am making an ingredient application where users paste ingredients
My application looks like this: 
As you can see, the first ingredient does not have an X at the end, because you must have at least one ingredient, but the rest of the ingredients. I also use JQuery Sortable Plugin , so if you click next to any of the ingredient ranges, you can reorder the ingredients. This works great unless you move the first range of ingredients, then this range does not have an X at the end, even if you move it to the last place.
So what I'm trying to do is that there is always no X in the first layer of ingredients , even if it switched to a different range of ingredients. I tried this:
$('ingredientsCOUNT > span:first').hide(deleteButton);
but it didn’t work? Any other suggestions? All help is much appreciated and here is my code:
HTML (php can just be ignored!)
<div class='formelementcontainer funky'>
<label for="ingredient">Ingredients</label>
<div id='ingredientsCOUNT' class='sortable'>
<span>
<input type="text" class='small' name="ingredient" id="ingredient" placeholder='QTY'/>
<select name='measurements'>
<option value='' name='' checked='checked'>--</option>
<?foreach ($measurements as $m):?>
<option value='<?=$m->id;?>'><?=$m->measurement;?></option>
<?endforeach;?>
</select>
<input type="text" name="ingredient" id="ingredient" placeholder='Ingredient'/>
</span>
</div>
<div class="clear"></div>
<div class='addSPAN tabover'>
<a class='float-right' id='btnAddIngredients' href='#'>Add Ingredient</a>
</div>
</div>
JQuery
(function($) {
$(document).ready(function () {
$('#btnAddIngredients').click(function () {
var num = $('#ingredientsCOUNT span').length;
var newNum = new Number(num + 1);
var deleteButton = $("<a class='float-right' style='margin:10px 2px;' href='#'><img src='<? echo base_url()."public/img/delete.png";?>' height='11' width='11' /></a>");
deleteButton.click(deleteThis);
$('#ingredientsCOUNT > span:first')
.clone()
.attr('name', 'ingredient' + newNum)
.append(deleteButton)
.appendTo('#ingredientsCOUNT')
.fadeIn();
$('ingredientsCOUNT > span:first').hide(deleteButton);
});
function deleteThis() {
var span = $(this).closest('span')
span.fadeOut('slow', function() { span.remove(); });
}
$( ".sortable" ).sortable();
});
})(jQuery);
source
share