look for element in parents

S

s k2999999

I want to find a parent for a button.
I have set up the html like the following.
<div id='user_123' class='user'>
<h1>My name goes here</h1>
<p>some description</p>
<a href='Javascript:{}' class='select'>
</div>

The thing is that I need to specify how many steps I have to go up to get
the
parent id( see the bottom ) and if I add another tag I migh have to change
this source. Is there any way to
specify using class ( maybe "$(this).parent(".user).attr("id") ?) so that I
don't need
to specify how many level I have to go up in order to get that id?

$().ready(function(){
$(".select").click(function(){
alert($(this).parent().attr("id"));
});
});

Thanks in advance.
 
D

Doug Gunnoe

I want to find a parent for a button.
I have set up the html like the following.
<div id='user_123' class='user'>
<h1>My name goes here</h1>
<p>some description</p>
<a href='Javascript:{}' class='select'>
</div>

The thing is that I need to specify how many steps I have to go up to get
the
parent id( see the bottom ) and if I add another tag I migh have to change
this source. Is there any way to
specify using class ( maybe "$(this).parent(".user).attr("id") ?) so thatI
don't need
to specify how many level I have to go up in order to get that id?

$().ready(function(){
$(".select").click(function(){
    alert($(this).parent().attr("id"));

});
});

Thanks in advance.

So if you add another tag in between the button and its parent, you
would want the button's parent's parent?

If that is the case, maybe you should rethink your approach. Because
if you're not really after the button's parent, but rather what just
so happens to be the button's parent at the moment, just give that
important node an id and use getElementById().

If I misunderstand, please clarify.

By the way, you might get some grief from others here for using
prototype or jQuery, or whatever that stuff is you posted. I'm not
sure what it is because I'm too young and uncorrupt.

Good luck.
 
T

Timo Reitz

s said:
I want to find a parent for a button.

No, the parent node is always the node directly "above" a node. You want to
find an ancestor.
I have set up the html like the following. <div id='user_123'
class='user'> <h1>My name goes here</h1> <p>some description</p> <a
href='Javascript:{}' class='select'> </div>

The thing is that I need to specify how many steps I have to go up to get
the parent id( see the bottom ) and if I add another tag I migh have to
change this source. Is there any way to specify using class ( maybe
"$(this).parent(".user).attr("id") ?) so that I don't need to specify how
many level I have to go up in order to get that id?

Yes (feature testing excluded):

function getIdByUserClassNode(node){
var pNode=node.parentNode;
if (pNode != null && pNode.nodeType==pNode.ELEMENT_NODE){
if (/^(.*\s+)?user(\s+.*)?$/.test(pNode.className)){
return pNode.id;}
else{
return getIdByUserClassNode(pNode);}}}

You can easily turn it into an iterative version.
$().ready(function(){ $(".select").click(function(){
alert($(this).parent().attr("id")); }); });

What is $? Its name doesn't say anything about its purpose.
 
R

Ricardo

I want to find a parent for a button.
I have set up the html like the following.
<div id='user_123' class='user'>
<h1>My name goes here</h1>
<p>some description</p>
<a href='Javascript:{}' class='select'>
</div>

The thing is that I need to specify how many steps I have to go up to get
the
parent id( see the bottom ) and if I add another tag I migh have to change
this source. Is there any way to
specify using class ( maybe "$(this).parent(".user).attr("id") ?) so thatI
don't need
to specify how many level I have to go up in order to get that id?

$().ready(function(){
$(".select").click(function(){
    alert($(this).parent().attr("id"));

});
});

Thanks in advance.

$(this).parents('.user:first').attr("id"); will get you div.user
regardless of how deeply nested the select is.

Head over to the jquery-en group if you need any further help on this.
 
R

Ricardo

No, the parent node is always the node directly "above" a node. You want to
find an ancestor.



Yes (feature testing excluded):

function getIdByUserClassNode(node){
   var pNode=node.parentNode;
   if (pNode != null && pNode.nodeType==pNode.ELEMENT_NODE){
     if (/^(.*\s+)?user(\s+.*)?$/.test(pNode.className)){
       return pNode.id;}
     else{
       return getIdByUserClassNode(pNode);}}}

You can easily turn it into an iterative version.


What is $? Its name doesn't say anything about its purpose.

I'm under the impression you do have a slight idea of what it does,
but anyway, it's a query function that accepts a CSS3 selector string
and returns an array-like object containing the matched elements and
several helper methods. It's named $ for quicker typing and avoid
cluttering code as it's used very often. In case you were to use a
framework like this and were bothered by it, you could rename it to
anything you judge clearer, no strings attached :)
 
D

David Mark

I'm under the impression you do have a slight idea of what it does,

I assume he knows better than you as you have not demonstrated much
understanding about jQuery.
but anyway, it's a query function that accepts a CSS3 selector string
and returns an array-like object containing the matched elements and
several helper methods. It's named $ for quicker typing and avoid

Hello? Macros.
cluttering code as it's used very often. In case you were to use a
Minification?

framework like this and were bothered by it, you could rename it to

Framework? It's a silly, ugly, unprofessional, broken-beyond-belief
50K blob of Javascript. Nothing more, nothing less. No wait, it's
lots of those. A new one comes out every other month.
anything you judge clearer, no strings attached :)

Except that your mind degenerates in a cult of incompetence.
 
D

David Mark

$(this).parents('.user:first').attr("id"); will get

There's that attr method. :)

Seeing as you use it in your demonstrations, perhaps you would care to
explain what it does. Not the code, but what is the method supposed
to do. Use the jQuery documentation if you have to, but it won't
really help.

Take a year. Resig's been pondering it for almost two.
you div.user
regardless of how deeply nested the select is.

I doubt it.
Head over to the jquery-en group if you need any further help on this.

LOL.
 
R

Ricardo

Hi David, first, let me thank you for your help and insightful
comments.
Seeing as you use it in your demonstrations, perhaps you would care to
explain what it does.  Not the code, but what is the method supposed
to do.  Use the jQuery documentation if you have to, but it won't
really help.

Take a year.  Resig's been pondering it for almost two.

attr() sets or retrieves an element's attribute or property. It
actually works in the real world.
I doubt it.

Good, why don't you try it for yourself?

You're such a nice person. cheers.
 
D

David Mark

Hi David, first, let me thank you for your help and insightful
comments.

If you only knew. :)
attr() sets or retrieves an element's attribute or property.

That's what the documentation says as well. Did you write it?
It actually works in the real world.

In order to determine if something works or not (regardless of the
world), it is necessary to define what constitutes a successful
operation. As your definition of the method is contradictory, there's
no way to keep score.

Why are you so quick to recommend a method you don't understand?
Good, why don't you try it for yourself?

Don't need to.
You're such a nice person. cheers.

I really am. You, on the other hand, are spreading misery without
even realizing it. Read more, write less.
 
D

David Mark

So why is it not called attrorproperty() ?

Likely because the author(s) did not realize what it does. The
trouble is, it would fill a pad full of graph paper to plot exactly
what attribute or property and what exactly would be set or received.
Can you prove that? For every browser? In the *real* world?

Prove what? That ever-shifting gibberish "works?" What constitutes a
success? Even if the only requirement is the lack of an exception,
the - attr - method is a dicey proposition at best. If it is to prove
that it does anything consistently from one version of IE (or jQuery)
to the next. I have proven that it does not work. Furthermore, it
does not contribute to the overall goal of jQuery to simplify
Javascript and DOM manipulation.

Odd for a method that is in virtually every jQuery example and
tutorial. Anyone who "learns" from such material is programming by
trial and error, building up a mental database of jQuery quirks, only
to be frustrated by the next (slightly different) revision. The root
of the problem, as has been shown repeatedly, is that the developers
don't know what is going on either. Also a method that could have
been avoided entirely, given a competent design.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top