最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Jquery html() method to get <li> html - Stack Overflow

matteradmin6PV0评论

I have an unordered list in my HTML

<ul>
   <li><div class="container1"><h2>list 1</h2></div></li>
   <li><div class="container2"><h2>list 2</h2></div></li>
   <li><div class="container3"><h2>list 3</h2></div></li>
</ul>

I want to get a particular <li> html using Jquery html() method

console.log($(".container1").closest('li').html()); // returns code from the div
output : <div class="container"><h2>list 1</h2></div>

I want the output as <li><div class="container1"><h2>list 1</h2></div></li> including <li> tags.

If I go one level above to closest('ul') its returning all the 3 li elements.

JSFiddle : /

I have an unordered list in my HTML

<ul>
   <li><div class="container1"><h2>list 1</h2></div></li>
   <li><div class="container2"><h2>list 2</h2></div></li>
   <li><div class="container3"><h2>list 3</h2></div></li>
</ul>

I want to get a particular <li> html using Jquery html() method

console.log($(".container1").closest('li').html()); // returns code from the div
output : <div class="container"><h2>list 1</h2></div>

I want the output as <li><div class="container1"><h2>list 1</h2></div></li> including <li> tags.

If I go one level above to closest('ul') its returning all the 3 li elements.

JSFiddle : http://jsfiddle/W4QfJ/417/

Share Improve this question asked May 28, 2015 at 17:02 budhavarapu rangabudhavarapu ranga 4833 gold badges7 silver badges15 bronze badges 1
  • Provided printing HTML content of that <li> in console is not the end-result here, what do you want to do with that <li> element next? If you lose the .html() at the end, you will have a perfectly good jQuery selector holding that same <li> element. You can do with it what you would do with any jQuery selection: remove it, move some place else, add after it, etc. – martynasma Commented May 28, 2015 at 18:03
Add a ment  | 

2 Answers 2

Reset to default 4

Try $(".container1").closest('li')[0].outerHTML (I think it is patible for most browsers-You probably need to check for IE)

So, this is more of a documentation than the answer as @tabz100 has the asnwer.

Few ways of getting the outer HTML (in the order of my preference):

// 1)
$(".container1").closest('li').prop("outerHTML");
// 2)
$(".container1").closest('li')[0].outerHTML;
// 3)
$(".container1").closest('li').wrap("<div></div>").parent().html();
Post a comment

comment list (0)

  1. No comments so far