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>









share|improve this question
























  • 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















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>









share|improve this question
























  • 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













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>









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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
})





share|improve this answer





















  • 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










  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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
})





share|improve this answer





















  • 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










  • 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

















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
})





share|improve this answer





















  • 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










  • 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















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
})





share|improve this answer












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
})






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 1:06









Jack Bashford

3,2073930




3,2073930












  • 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










  • 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




















  • 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










  • 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


















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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine