I',m quite new with jQuery and I face a problem.
When I try to delete elements at the beginning it works, but when I add new element and then try to delete it, other siblings li elements are deleted too. How Can i Solve it?
That's basic structure:
<br/>
<b>Delete</b>
<input type="radio" id="delnod" name="action" title="Delete"/>
<br/>
<b>Add</b>
<input type="radio" id="addnod" name="action" title="Add"/>
<br/>
<ul id="rodzic">
<li id="xx">XX
<ul id="cv">
<li id="vb">aa</li>
<li id="hh">vv</li>
<li id="fg">cc</li>
</ul>
</li>
<li id="yy">YY
<ul id="zz">
<li id="vv">12</li>
<li id="vx">34</li>
<li id="vz">55</li>
</ul>
</li>
</ul>
And that's jQuery code:
$(document).ready(function() {
$("ul,li").click(function() {
if ($('#delnod').is(':checked'))
{
if (confirm('Do you want to delete?'))
{
$(this).remove();
return false;
}
}
if ($('#addnod').is(':checked'))
{
$("<li>And me too!</li>").insertAfter(this);
// $(this).closest('ul').children(':last').before('<li />');
return false;
}
});
});