How to find neighbouring element of mother element (jQuery)?
up vote
1
down vote
favorite
When I'm clicking on li
element, I want to work with elem2
and it must to work with $(this)
because I have a lot wrappers.
<div class='wrapper'>
<div class='elem1'>
<ul>
<li></li>
</ul>
</div>
<div class='elem2'></div>
</div>
javascript jquery
add a comment |
up vote
1
down vote
favorite
When I'm clicking on li
element, I want to work with elem2
and it must to work with $(this)
because I have a lot wrappers.
<div class='wrapper'>
<div class='elem1'>
<ul>
<li></li>
</ul>
</div>
<div class='elem2'></div>
</div>
javascript jquery
what exactly you want ? on clicking of li element, you want to select all child element of wrapper ? or you just want to select all siblings of elem1 i.e.e elem2 elem3 etc..
– Girish
Nov 11 at 0:55
Try$(this).closest('div').next()
. Would help a lot if you gave more details as in How to Ask
– charlietfl
Nov 11 at 1:04
last one :) i just want to select elem2
– hitt
Nov 11 at 1:05
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
When I'm clicking on li
element, I want to work with elem2
and it must to work with $(this)
because I have a lot wrappers.
<div class='wrapper'>
<div class='elem1'>
<ul>
<li></li>
</ul>
</div>
<div class='elem2'></div>
</div>
javascript jquery
When I'm clicking on li
element, I want to work with elem2
and it must to work with $(this)
because I have a lot wrappers.
<div class='wrapper'>
<div class='elem1'>
<ul>
<li></li>
</ul>
</div>
<div class='elem2'></div>
</div>
javascript jquery
javascript jquery
edited Nov 11 at 4:54
Shiladitya
9,34391830
9,34391830
asked Nov 11 at 0:30
hitt
85
85
what exactly you want ? on clicking of li element, you want to select all child element of wrapper ? or you just want to select all siblings of elem1 i.e.e elem2 elem3 etc..
– Girish
Nov 11 at 0:55
Try$(this).closest('div').next()
. Would help a lot if you gave more details as in How to Ask
– charlietfl
Nov 11 at 1:04
last one :) i just want to select elem2
– hitt
Nov 11 at 1:05
add a comment |
what exactly you want ? on clicking of li element, you want to select all child element of wrapper ? or you just want to select all siblings of elem1 i.e.e elem2 elem3 etc..
– Girish
Nov 11 at 0:55
Try$(this).closest('div').next()
. Would help a lot if you gave more details as in How to Ask
– charlietfl
Nov 11 at 1:04
last one :) i just want to select elem2
– hitt
Nov 11 at 1:05
what exactly you want ? on clicking of li element, you want to select all child element of wrapper ? or you just want to select all siblings of elem1 i.e.e elem2 elem3 etc..
– Girish
Nov 11 at 0:55
what exactly you want ? on clicking of li element, you want to select all child element of wrapper ? or you just want to select all siblings of elem1 i.e.e elem2 elem3 etc..
– Girish
Nov 11 at 0:55
Try
$(this).closest('div').next()
. Would help a lot if you gave more details as in How to Ask– charlietfl
Nov 11 at 1:04
Try
$(this).closest('div').next()
. Would help a lot if you gave more details as in How to Ask– charlietfl
Nov 11 at 1:04
last one :) i just want to select elem2
– hitt
Nov 11 at 1:05
last one :) i just want to select elem2
– hitt
Nov 11 at 1:05
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You could use this jQuery:
$("li").click(function() {
var elem2 = $(this).closest("div").next();
//Do stuff
})
Or:
$("li").click(function() {
var elem2 = $(".elem2");
//Do stuff
})
what if i have one morediv
betweenelem1
andelem2
?
– hitt
Nov 11 at 1:15
Then use the second example
– Jack Bashford
Nov 11 at 2:36
ok, if i have a lot tags with classelem2
, but I need to work withelem2
of thisli
?
– hitt
Nov 11 at 12:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You could use this jQuery:
$("li").click(function() {
var elem2 = $(this).closest("div").next();
//Do stuff
})
Or:
$("li").click(function() {
var elem2 = $(".elem2");
//Do stuff
})
what if i have one morediv
betweenelem1
andelem2
?
– hitt
Nov 11 at 1:15
Then use the second example
– Jack Bashford
Nov 11 at 2:36
ok, if i have a lot tags with classelem2
, but I need to work withelem2
of thisli
?
– hitt
Nov 11 at 12:07
add a comment |
up vote
0
down vote
accepted
You could use this jQuery:
$("li").click(function() {
var elem2 = $(this).closest("div").next();
//Do stuff
})
Or:
$("li").click(function() {
var elem2 = $(".elem2");
//Do stuff
})
what if i have one morediv
betweenelem1
andelem2
?
– hitt
Nov 11 at 1:15
Then use the second example
– Jack Bashford
Nov 11 at 2:36
ok, if i have a lot tags with classelem2
, but I need to work withelem2
of thisli
?
– hitt
Nov 11 at 12:07
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You could use this jQuery:
$("li").click(function() {
var elem2 = $(this).closest("div").next();
//Do stuff
})
Or:
$("li").click(function() {
var elem2 = $(".elem2");
//Do stuff
})
You could use this jQuery:
$("li").click(function() {
var elem2 = $(this).closest("div").next();
//Do stuff
})
Or:
$("li").click(function() {
var elem2 = $(".elem2");
//Do stuff
})
answered Nov 11 at 1:06
Jack Bashford
3,2073930
3,2073930
what if i have one morediv
betweenelem1
andelem2
?
– hitt
Nov 11 at 1:15
Then use the second example
– Jack Bashford
Nov 11 at 2:36
ok, if i have a lot tags with classelem2
, but I need to work withelem2
of thisli
?
– hitt
Nov 11 at 12:07
add a comment |
what if i have one morediv
betweenelem1
andelem2
?
– hitt
Nov 11 at 1:15
Then use the second example
– Jack Bashford
Nov 11 at 2:36
ok, if i have a lot tags with classelem2
, but I need to work withelem2
of thisli
?
– hitt
Nov 11 at 12:07
what if i have one more
div
between elem1
and elem2
?– hitt
Nov 11 at 1:15
what if i have one more
div
between elem1
and elem2
?– hitt
Nov 11 at 1:15
Then use the second example
– Jack Bashford
Nov 11 at 2:36
Then use the second example
– Jack Bashford
Nov 11 at 2:36
ok, if i have a lot tags with class
elem2
, but I need to work with elem2
of this li
?– hitt
Nov 11 at 12:07
ok, if i have a lot tags with class
elem2
, but I need to work with elem2
of this li
?– hitt
Nov 11 at 12:07
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244762%2fhow-to-find-neighbouring-element-of-mother-element-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what exactly you want ? on clicking of li element, you want to select all child element of wrapper ? or you just want to select all siblings of elem1 i.e.e elem2 elem3 etc..
– Girish
Nov 11 at 0:55
Try
$(this).closest('div').next()
. Would help a lot if you gave more details as in How to Ask– charlietfl
Nov 11 at 1:04
last one :) i just want to select elem2
– hitt
Nov 11 at 1:05