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)
{

}
8
  • yes that condition is correct.. what do you want..? Commented Jun 25, 2014 at 5:37
  • possible duplicate of Check if element exists in jQuery Commented Jun 25, 2014 at 5:37
  • This is perfectly right to check if div exist or not. and it seems to be working fine. what is your problem now? Commented Jun 25, 2014 at 5:37
  • 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. Commented Jun 25, 2014 at 5:39
  • 1
    Here is your working fiddle jsfiddle.net/e56TY/41 Commented Jun 25, 2014 at 5:39

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

$(document).on("click", ".activateUiHTML .checkboxclas", function (e) {
})

Comments

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) {...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.