Solved

JQuery, Select an elements parent element

Posted on 2008-10-07
4
6,378 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
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 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