Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery, Select an elements parent element

Posted on 2008-10-07
4
Medium Priority
?
6,383 Views
Last Modified: 2012-08-13
I have the code below which in JQuery starts with a ul that has a specific ID

then gets each child li of that ul. I will be doing more here on the li element... later

then it gets each child up of each specific li element.. again, I will be doing operation on each li

but now I have a specific li element and I need to get the following information

1) The width of its parent li element
2) The height of its parent li element

Where it says alert(this); is where I have each ul element.
$('ul#RightSliderMenu').each(function(){
    $(this).children('li').each(function(){
      // perform operation on li element
      $(this).children('ul').each(function(){
        // perform operation on ul element
        alert(this);
      });
    });
  });

Open in new window

0
Comment
Question by:Hube02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22663004
$(this).parent() should give you the parent element of whatever "this" refers to at that point.
0
 
LVL 18

Author Comment

by:Hube02
ID: 22663090
Thanks Hielo, I'm learning it one step at a time... It does not help that I want to do something that is not covered in the books I have. They have a lot of stuff in there about applying classes to elements with JQuery, but not about setting css style using it.

this is what I have to obtain the information I wanted...
  $('ul#RightSliderMenu').each(function(){
    $(this).children('li').each(function(){      // the "this" in this line is the element ul#RightSliderMent
      $(this).children('ul').each(function(){    // the "this" in this line is the element ul#RigntSliderMenu>li
        alert(this);                            // the "this" in this line is the element ul#RightSliderment>li>ul
        alert($(this).parent().css('width'));
      });
    });
  });

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22663173
>>but not about setting css style using it.
To set some properties:
$(this).css( { backgroundColor:"yellow", fontWeight:"bolder" } );

to find out a property:
$(this).css( "background-color");

You don't have to use "this", if you know the element id:
$("#theId").css(....);

However, I believe the css() will report the css as applied via style="...". Forget jQuery for a second and look at the attached code. Try it. Then remove the style attribute, try it again. See the difference. Hope that makes sense.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script>
window.onload=init;
function init(){
alert(document.getElementById("theDiv").style.width);
}
</script>
</head>
<body>
<div id="theDiv" style="width:50%;">Hello</div>
</body>
</html>

Open in new window

0
 
LVL 18

Author Comment

by:Hube02
ID: 22667869
The problem is that past the id of the main list, I have nothing to go with. there will be no classes or id's on these li and ul elements. In the end the process is going to great an animation that will slide navigation menus out of the right side of the screen, but to keep it accessible, I will be using JQuery(JavaScript) to apply all the initial styles as well as to created the click/mouseover animations. This way if someone visits the site with JS turned off, the will still be able to see the navigation, where if I set the styles in a style sheet and had hidden elements, they would never be able to get to these elements because they are controlled with JS.

Another reason is that we (as in the people I work for) have decided to go with JQuery as a standard package. Rather than using several different things on different sites, we are going to use a standard set of tools, JQuery just happens to be one of those tools in the list.

I still appreciate the input.
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question