Determine if a UL has 1 or more LI within - javascript

How can I use JavaScript to determine if a UL contains 1 or more LI's within?
Pseudo code
if('ul#items' has >= 1 LI){
Thanks,

With jQuery:
$('ul#items li').length >= 1
Without jQuery:
document.getElementById('items').getElementsByTagName('li').length >= 1

With jQuery:
if( $('#items li').length >= 1 ){...

Use document.getElementById("items").childNodes.length and a number comparison operator. If your ul does contain other nodes than li, you will have to filter them.
In jQuery:
$("#items").children("li").length
I guess you only want direct children, so don't use find().

if ($('ul#items').children('li').length) {
// etc.
}

You can use:
$('ul#items li').length

If you're using jQuery
if($('ul > li').size()>0) {
}
That will check that the UL element has more than 0 direct child li elements.

if ($('ul#items > li').length >= 1)

Related

Hide div if the ul is empty

Here is my html,
<div id="personaldetails">
<ul>
<li class="clear"></li>
</ul>
<ul>
<li class="clear"></li>
</ul>
</div>
I want to hide div personaldetails when all the ul inside in div is empty.
If the ul is having element <li class="clear"></li> then the ul is considered as to be empty.
How to do this using Jquery ?
You can try this:
$('#personaldetails').find('ul').each(function(){
var txt = $("li", this).text();
if(txt.length <= 0){
$(this).hide();
}
});
if(!$('#personaldetails').find('ul:visible').length){
$('#personaldetails').hide();
}
Updated Fiddle
And to me you should hide all ul, if no ul are visible then you can hide the #personaldetails div.
Even one of answer is already accepted, I think it can be simple as:
if($.trim($("#personaldetails").text()) == '') {
$("#personaldetails").hide();
}
:)
Take a look at that code:
function foo(){
var all_li_clear = true;
$("#personaldetails > ul > li").each(function(){
if(!$(this).hasClass("clear")){
all_li_clear = false;
break; // No need to continue now
}
});
if(all_li_clear){
$("#personaldetails").hide();
}
}
You can see a fiddle example there, just comment and discomment foo(); line.
Javascript solution:
This will only hide the div if all li have clear class
$(function() {
emptyLi = $('#personaldetails ul li').filter(function(){
/*if($(this).hasClass('clear')){
return true;
}else{
return false;
}*/
return $(this).hasClass('clear');
}).length;
if($('#personaldetails ul li').length == emptyLi){
$('#personaldetails').css('display','none');
}
});
CSS:
This will hide the li with class clear, so if you not fixed height of ul or li and don't have padding , margin given to ul,li your div personaldetails will get hidden automatically when all li element have class clear
#personaldetails ul li.clear{
display:none;
}
-UPDATED-
You can use following code if you are deciding empty class based on clear class.
if($("#personaldetails ul li:not(.clear)").length == 0) {
$("#personaldetails").hide();
}
Or if you are looking for the empty div then you can just use the shortest code given by #Samiul Amin Shanto Like:
if($.trim($("#personaldetails").text()) == '') {
$("#personaldetails").hide();
}
Explanations
Method1:
$("#personaldetails ul li:not(.clear)")
This code find all li without the clear class. Then if no such li found, just hide the div. Fiddle
Method2:
$("#personaldetails").text() this code return innerHTML text striping all html tags. So no meter what the div contain ul, li or anything else, this will return the plain text content of the div, then striping any white space we can determine if the div is empty. If your intention is to hide the empty div not hiding the div which contain empty Ul this should be your choice.
This asumes that if you have the same amount of li's with the class clear, as there are ul's, they're all empty
var $wrapper = $('#personaldetails');
if( $wrapper.find('ul').length=== $wrapper.find('li.clear').length){
$wrapper .hide();
}
Everybody's fiddling examples :)
$(function($) {
$cnt = 0;
$('.personalDetails ul li').each(function() {
if($(this).hasClass('clear')) $cnt++;
});
if($('.personalDetails ul li').length == $cnt) $('.personalDetails').hide();
});
$("ul li:empty").closest('div#personaldetails').hide();
Sample Code
#personaldetails ul li.clear{
visibility:hidden;
}

jQuery selecting top level element text

I have a menu that has the section Store this has a submenu of categories Printers Keyboards and under these categories they have their own sub categories.
What I am trying to do is select the top level categories that are Printers Keyboards just using jQuery into an Array.
When I use the following it prints out ["Printers", "Color", "Black/White", "Dual", "Keyboards", "Wired", "Wireless", "Touchscreen"] This is more than I need for now, could someone help me
Check out my jsFiddle
jQuery
var optionTexts = [];
$('.main-menu ul li a:contains("Store")').parent().find('.sub-menu > li a').each(function(){
optionTexts.push($(this).text());
});
console.log(optionTexts);
EDIT I looked at your fiddle a bit more closeley and changed the way the right "a" tag is found:
'.sub-menu > li > ul > li > a' will do it (I checked it in your jsfiddle):
$('.main-menu ul li a:contains("Store")').parent().find('.sub-menu > li > ul > li > a').each(function(){
optionTexts.push($(this).text());
});
The way you did it, every a underneath the "ul.sub-menu" is selected - also those that are nested in children and grandchildren of "ul.sub-menu".
Hope you are looking for something like this
var optionTexts = [];
$('.main-menu ul li a:contains("Store")').parent().find('.sub-menu').parent('li').each(function(){
optionTexts.push($(this).children('a').text());
});
console.log(optionTexts);
I Edit your code like this, its worked :
$(function () {
$(".check").on('click', function () {
var optionTexts = [];
$('.list > ul > li > a').each(function () {
optionTexts.push($(this).text());
});
c(optionTexts);
});
});
output : Printers, Keyboards
Check it FIDDLE : http://jsfiddle.net/mehmetakifalp/Mkv9q/16/

target single element when it is not possible to use $(this)

function modalClosed(){
$("div#tab" + tabId).find('ul')
.prepend("<li>item</li>")
.hide()
.fadeIn('slow');
}
I want the list (<li>) to be prepended and have fade in effect one by one, unfortunately I have no way of using $(this), the above code doesn't work well, it apply effect on all of the <li>.
That's because .prepend() returns the ul element not the appended li element, so you are hiding/showing the ul element. You can reverse the logic using prependTo() method, now .hide() and .fadeIn() are applied to the appended element not the ul element.
$("<li>item</li>").hide()
.prependTo("#tab"+tabId+" ul")
.fadeIn('slow');
http://jsfiddle.net/5yj7v/
function modalClosed(num) {
$("div#tab" + tabId).find('ul')
.prepend("<li>item</li>")
.hide()
.fadeIn('slow', function() {
// callback function, called when fadeIn has finished
if(num > 1) {
modalClosed(num - 1);
}
});
}
From what I understand You want fade in effect one by one on all li items. If that is correct then try the following:
var time=1000;
$("div#tab"+tabId+" ul").prepend("<li>item</li>");
$("div#tab"+tabId+" ul li").each(function() {
$(this).hide();
$(this).fadeIn(time);
time+= 800;
});
http://jsfiddle.net/PV4dC/5/

Remove DIV tag using Javascript or Jquery

How do I remove a DIV with a specific value?
<div value="0" class="task_row"></div>
I want to remove the above div which has value 0.
As Ben Rowe points out in the comments, value is not a valid attribute of the div tag. And both the jQuery solution and the solution that uses getElementsByTagName() has to iterate through a list, which is bad for performance. I think that creating an id attribute instead is a better option:
<div id="task_row_0" class="task_row"></div>
And then you can just do:
var div = document.getElementById("task_row_" + taskId);
div.parentNode.removeChild(div);
this is jquery code )):
$('div').each(function(){
if($(this).attr('value') == '0'){
$(this).remove();
}
});
var divs = document.getElementsByTagName('div');
for(var i = divs.length; i; i -= 1) {
if (divs[i].getAttribute('value') == 0) {
divs[i].parentNode.removeChild(divs[i]);
}
}
Edit: Nevermind - Zhasulan beat me to it. :P
With jQuery -
$('div').each(function(){
if($(this).attr('value') == '0') {
$(this).hide();
}
});
Alternative to jQuery/JavaScript you can achieve it via CSS only -
JSFIDDLE
div[value="0"] {
display: none;
}
Or via jQuery using attribute selector:
JSFIDDLE
$("div[value='0']").hide(); /*.remove() as per your requirement*/

Full selector string or this.find and selector string

Both work, but is there any advantage / dis-advantage of using one over the other ?
$("#" + this.id + " > ul > li.active:first")
vs
$(this).find(" > ul > li.active:first")
The second selector works, I'd go with that instead of concatenating the ID into the first selector (looks ugly) and making jQuery look for that same element ID again (redundant).
You could also do this... I think.
$(" > ul li.active:first", this);

Categories

Resources