Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends,

Thanks for your time,

i need help, now im creating div(with controls) dynamically, and also i have remove option for that, so im creating dynamically remove option(just link) also.

now i want to check condition like, user cannot remove every div,limit is 3 times, and also need to set alert message if they reach limit. pls find below my code

html
=====
HTML
<div class="span3" style="margin-removed270px"><a href="#x" id="removelink">Remove</a></div>


script
=======
JavaScript
<script type="text/javascript">
$(".form-horizontal").delegate("#removelink", "click", function () {
    $(this).closest('.optiondiv').hide('slow', function(){
        $(this).remove();
        resequence();
        });
    });
</script>

pls help me to solve this problem.

thanks :)
Posted
Updated 23-Sep-14 15:55pm
v2

Use hidden field to keep track of how many divs are removed.

HTML
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function(){
$("#removelink").live("click", function () {
var currentVal = parseInt($("#counter").val(),10);
if(currentVal >=3) { alert("Ninja Attack - You cannot remove more than 3 divs !!!"); return;}
    $(this).closest('.o').hide('slow', function(){
        $(this).remove();

         currentVal+=1;
         $("#counter").val(currentVal);
        });
    });
});
</script>
<div class="o" >
<a href="#x" id="removelink">Remove</a></div>
<div class="o" >
<a href="#x" id="removelink">Remove</a></div>
<div class="o" >
<a href="#x" id="removelink">Remove</a></div>
<div class="o" >
<a href="#x" id="removelink">Remove</a></div>

<input type="hidden" id="counter" value="0"/>

</body>
</html

>
 
Share this answer
 
v2
Comments
Renga.g 23-Sep-14 23:58pm    
hi thanks its working with new trick.

i need one more help same this code, i want to check user cannot remove all option, they must leave atleast 2 options.

kindly pls help me :)
Renga.g 24-Sep-14 0:06am    
hi Thank you very much, its working very good :)

kindly pls one more help,i want check user can remove all option, but must they leave any 2 options.
Renga.g 24-Sep-14 23:05pm    
have any idea for this?
Please follow the below steps to achieve the required result
1. Take a variable name it maxdelcount assign the value 3 to it.
2. Take another variable delcount assign the value 0 to it.
3. Check the condition if delcount is less than maxdelcount before this line "$(this).remove()". if delcount is less than maxdelcount the remove the div and increment delcount.
4. If delcount is greater than or equal to maxdelcount then display the alert message.

Hope the above steps help

JavaScript
<script type="text/javascript">
var maxdelcount=2;
var delcount=0;
$(".form-horizontal").delegate("#removelink", "click", function () {
$(this).closest('.optiondiv').hide('slow', function(){
if (delcount < maxdelcount)
{
$(this).remove();
resequence();
delcount++;
}
else if (delcount >= maxdelcount)
{
alert("hi");
}
});

});
</script>
 
Share this answer
 
v2
Comments
Renga.g 23-Sep-14 22:37pm    
sorry! can you pls send me any sample code for this.
Renga.g 23-Sep-14 22:56pm    
i try ready,im not sure where its going wrong. :(
ChauhanAjay 23-Sep-14 23:03pm    
can you kindly update your code here.
Renga.g 23-Sep-14 23:10pm    
<script type="text/javascript">
$(".form-horizontal").delegate("#removelink", "click", function () {
$(this).closest('.optiondiv').hide('slow', function(){
var maxdelcount=2;
var delcount=0;
if (delcount < maxdelcount)
{
$(this).remove();
resequence();
}
else if (delcount >= maxdelcount)
{
alert("hi");
}
else
{
}
});

});
</script>
ChauhanAjay 23-Sep-14 23:23pm    
I have update the solution with the code kindly test and let me know the result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900