I am having trouble solving a probelm with jQuery. Here is the problem definition.
I have a document as below.
<DIV id="A1" class="initial">Bla Bla Bla</DIV>
<DIV id="A1_B1" class="comparing">Foo Foo Foo</DIV>
<DIV class="classAdded">
<DIV id="A2" class="initial">Bla Bla Bla</DIV>
</DIV>
<DIV id="A2" class="initial">Bar Bar Bar</DIV>
<DIV class="newClass">someData</DIV> ---> This class gets created/ or even if it exits
<DIV id="A2_B1" class="comparing">Button</DIV>
<DIV id="A2_B2" class="comparing">Foo Foo Foo</DIV>
When "Button" is cicked (Button can be clicked multiple times), I have to find all the DIV's with id = A2. When found, I have to check if the immediate next div has a class = "newClass". If not then add/ create the div with this class = "newClass" and add "someData" inside it. Else if the class is found then add/ append "someData" inside it.
But the only condition of this whole process is the A2 cannot have the parent class = "classAdded"
This is my approach
if($('#A2:not(.classAdded)').next().hasClass("newClass"))
$('#A2').next(".newClass").append(someData);
else
{
var abc = "<div class='newClass'>"+someData+"</div>";
$(abc).insertAfter('#A2');
}
What happens is, it is always createing the class next to id = "A2" whose parent is class="classAdded" like below - which is not wanted, because parent class cannot be class="classAdded"
<DIV id="A1" class="initial">Bla Bla Bla</DIV>
<DIV id="A1_B1" class="comparing">Foo Foo Foo</DIV>
<DIV class="classAdded">
<DIV id="A2" class="initial">Bla Bla Bla</DIV>
<DIV class="newClass">someData</DIV>
</DIV>
<DIV id="A2" class="initial">Bar Bar Bar</DIV>
<DIV class="newClass">someData</DIV> ---> This class gets created/ or even if it exits
<DIV id="A2_B1" class="comparing">Button</DIV>
<DIV id="A2_B2" class="comparing">Foo Foo Foo</DIV>
Even if we consider the sitution that
<DIV class="newClass">someData</DIV> exits
Then someData should be added inside it. But instead it creates a new DIV inside the parent class="classAdded"