1

If i click on checkbox under activateUiHTML div , it is appending that particular div to an another div present inside My Orders div

However if i uncheck the checkbox and recheck the checkbox under activateUiHTML , it is adding the same elemnt to the My Orders div again .

This is my jsfiddle

http://jsfiddle.net/e56TY/40/

I tried to put this condition , but not sure if this is correct or not

if($('#addtoordersdiv'+id_attr_val).length > 0)
{

}
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • yes that condition is correct.. what do you want..? – Rajaprabhu Aravindasamy Jun 25 '14 at 05:37
  • possible duplicate of [Check if element exists in jQuery](http://stackoverflow.com/questions/4592493/check-if-element-exists-in-jquery) – user229044 Jun 25 '14 at 05:37
  • This is perfectly right to check if div exist or not. and it seems to be working fine. what is your problem now? – Bhushan Kawadkar Jun 25 '14 at 05:37
  • @RajaprabhuAravindasamy , i don't know why its adding the same element again under My Orders div . – Pawan Jun 25 '14 at 05:38
  • 1
    The reason of your problem is duplicate id. See the id of your original checkbox, and the copied checkbox also has same id. You need to give different id, for example id_Selected etc. – PM. Jun 25 '14 at 05:39
  • 1
    Here is your working fiddle http://jsfiddle.net/e56TY/41/ – Shaunak D Jun 25 '14 at 05:39
  • Thanks Shaunak D , your fiddle is exactly what i needed , that if condition is perfect . Thanks once again . – Pawan Jun 25 '14 at 05:48
  • But when we try to check the first checkbox again after unchecking it, its not checking the duplicate checkbox back... is that what you really need? – Tejasva Dhyani Jun 25 '14 at 05:58

3 Answers3

1

Fiddle

Add an additional condition after your fetch ID,

if ($(this).is(':checked')) {

    var id_attr_val = $(this).attr("id");
    if(!$('#addtoordersdiv'+id_attr_val).length){

         //append code

    }

.length returns 0 if element does not exists.

Alternative to !$(elem).length you can also use $(elem).length === 0

Community
  • 1
  • 1
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
0

You can add the handler to the checkboxes inside the active div only like

$(document).on("click", ".activateUiHTML .checkboxclas", function (e) {
})
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

you can try this: http://jsfiddle.net/e56TY/43/

check if it already has the element or not:

if ($(this).is(':checked') && $("#ordersdiv").children('#addtoordersdiv'+id_attr_val).length==0) {...
Amin Jafari
  • 7,157
  • 2
  • 18
  • 43