473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Add class depending if LI has another tag in it

384 256MB
I'm trying to figure out how I would got about adding a class into an LI depending if a UL tag existed within the LI. I know how to add the class, my issue is how do I check for a UL within an LI?
Oct 5 '10 #1

✓ answered by ziycon

Ended up using the below code which works fine.
Expand|Select|Wrap|Line Numbers
  1. $("div#container ul li:contains('<ul>')").addClass("closed");

7 2442
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. // {li} being the <li> element
  2. if ({li}.getElementsByTagName("UL").length > 0)
Oct 5 '10 #2
ziycon
384 256MB
Sorry about all the questions, do I use it literally like:
Expand|Select|Wrap|Line Numbers
  1. if ({div#container ul li}.getElementsByTagName("UL").length > 0)
or
Expand|Select|Wrap|Line Numbers
  1. if (("div#container ul li").getElementsByTagName("UL").length > 0)
Oct 5 '10 #3
Dormilich
8,658 Expert Mod 8TB
no, you don’t. if you’re using jQuery, the syntax looks different.
Oct 5 '10 #4
ziycon
384 256MB
I'm using this code and it does what I want but it adds the class to LIs that don't have a UL inside them, it looks like it checks the first one and applies it to all LIs instead of checking each LI separately, any ideas?
Expand|Select|Wrap|Line Numbers
  1. if ($("div#container ul li ul").length > 0){
  2.     $("div#container ul li").addClass("CollapsiblePanelClosed menu_Closed");
  3.   }
Oct 5 '10 #5
JKing
1,206 Expert 1GB
If you are going to use jquery why not implement the :has() selector.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  4. </head>
  5. <body>
  6.     <div id="container">
  7.         <ul>
  8.             <li>Item 1</li>
  9.             <li>Item 2</li>
  10.             <li>Item 3
  11.                  <ul>
  12.                     <li>Sub Item 1</li>
  13.                 </ul>
  14.             </li>
  15.             <li>Item 4</li>
  16.             <li>Item 5
  17.                  <ul>
  18.                     <li>Sub Item 1</li>
  19.                 </ul>
  20.             </li>
  21.         </ul>
  22.     </div>
  23.     <script type="text/javascript">$("div#container ul li:has(ul)").addClass("CollapsiblePanelClosed");</script>
  24. </body>
  25. </html>
  26.  
Oct 5 '10 #6
ziycon
384 256MB
Ended up using the below code which works fine.
Expand|Select|Wrap|Line Numbers
  1. $("div#container ul li:contains('<ul>')").addClass("closed");
Oct 5 '10 #7
kovik
1,044 Expert 1GB
That won't work if the <ul> element has any attributes (class, id, etc.). jQuery's :has selector is what you should use, not :contains.
Oct 5 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Wilfried Mestdagh | last post by:
Hi, When do you put a class in another class and when not ? It seems in most case both work fine, but in some case not. I'm not yet smart enough in dotnet to know when and wy. Can someone...
1
by: Nick Zdunic | last post by:
In 1.1 it was possible to refer to a web page class from another class I've just started in v2.0 and found that this code sn't working The web page class: Partial Class _Default Inherits...
4
by: Serdge Kooleman | last post by:
hi how to use my class from another class (.Net 2005) i have web project (one page) and created a class MyClass in separate file, the same folder, included in project.
2
by: KraftDiner | last post by:
I have a class that is defined in a file called MyClass.py How do I use that class in another python script.. import MyClass ? (Does it need to be in a specific location?)
4
by: psbasha | last post by:
Hi, How to declare a Class inside another class as avariable? For Example" Class X{ MethodX1();
3
by: deduction | last post by:
How do I assign one class to another? Such as ..class_a { .class_b }
1
by: mahendra23 | last post by:
Can one constructor of a class call another constructor of the same class to initialize the this object?
6
by: ffreino | last post by:
Hi, I have a Solution with two different Projects. In one project, can I import a class from another project? It's something like: Solution mySolution ProjectA Class A1
2
by: muzaffaralirana | last post by:
I want to know that how I can View/Retrieve data from one C# Class(e.g. "abc.cs") in another C# Class(e.g. "def.cs") in same project ? I have to access the variables from one class to another...
8
by: hoopy | last post by:
Hi, Im just learning OO in PHP and not sure the best method to use the functions for example in a database wrapper class within another class for example which handles all user authentication. I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.