Reputation: 352
I have <div>
elements which has more <div>
in it and others which has no <div>
in it. I want name of the divs one by one .but if a div has another div in it I don't want the name of the .maindiv
but I want the name of .seconddivs
which is inside of the .maindiv
.
I draw a scheme for my qustion:
I will use jquery for this mission so here my code:
$.each($("body").find(".maindiv"), function(e) {
if($(this).find(".seconddivs") == false){
$(".suggest_for_column").append("<option>"+$(this).children(".nameofthediv").text()+"</option>"); // in this line of code i will append the .nameofthediv (name of my div) to inside a <select> element
}else{
$(".suggest_for_column").append("<option>"+$(this).find(".seconddivs").children(".nameofthediv").text()+"</option>");
}
})
I don't know where this code is wrong, but it is not working at all( nothing happens )
Upvotes: 1
Views: 45
Reputation: 1074158
find
doesn't return true or false, it returns a jQuery object containing any matching elements.
So you can check its length
:
$.each($("body").find(".maindiv"), function(e) {
var secondDivs = $(this).find(".seconddivs");
if(secondDivs.length){
// It has them, use them
}else{
// It doesn't have them
}
});
I removed the code in the clauses because it has syntax errors. You're using .
incorrectly, I suspect you meant to use +
(for string concatenation).
Side note: The usual way to write $.each($("body").find(".maindiv"), function{})
is $(".maindiv").each(function{})
.
Upvotes: 2