?
Solved

jQuery: Append to last <li> in string

Posted on 2013-11-25
4
Medium Priority
?
510 Views
Last Modified: 2013-11-25
How can I append to the last <li> inside a string?  I don't ever want the string to be on the page.  It should stay a string.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Examples</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {

 var xyz='<li>1</li><li>2</li><li>3</li>';
 xyz = $(xyz).find('li').last().append(' <span>LIST LI ITEM</span> ');
 alert(xyz);

 var xyz='<div><ul><li>1</li><li>2</li><li>3</li><ul><div>';
 xyz = $(xyz).find('li').last().append(' <span>LIST LI ITEM</span> ');
 alert(xyz);


});

</script>

</head>
<body>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:Mohit Vijay
Mohit Vijay earned 200 total points
ID: 39675750
In a simple way, you can use of lastIndexOf and substr

using lastIndexOf get the index of last occuring of </li> and once you got the index, use substr and then append remaining text.

makes sense?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39675850
Test page : http://jsfiddle.net/7vXxq/
$(document).ready(function () {

     var xyz ='<li>1</li><li>2</li><li>3</li>';
     var xyz = $("<div/>").html($(xyz).append(' <span>LIST LI ITEM</span> ')).html();
     alert(xyz);
    
     var xyz = '<div><ul><li>1</li><li>2</li><li>3</li><ul></div>';
     var xyz = $("<div/>").html($(xyz).find("li").append(' <span>LIST LI ITEM</span> ').parents(":last")).html();;
     alert(xyz);

});

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 1400 total points
ID: 39676452
new one : http://jsfiddle.net/7vXxq/1/

$(document).ready(function () {

    var span = ' <span>LIST LI ITEM</span> '; 
    var el = "li";
    var xyz = "";
    
     xyz = '<li>1</li><li>2</li><li>3</li>';
     xyz = appendToStr(xyz, el, span);
     alert(xyz);
    
     xyz = '<div><ul><li>1</li><li>2</li><li>3</li></ul></div>';
     xyz = appendToStr(xyz, el, span)
     alert(xyz);

});

// THANKS GaryC123
function appendToStr(str, strElement, strNode) {
    return $("<div>" + str + "</div>").find(strElement + ":last").append(strNode).parent().html();
}

Open in new window

0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 400 total points
ID: 39676477
Another option if you need it :)

var xyz='<li>1</li><li>2</li><li>3</li>';
newString = $('<div>').append( $.parseHTML(xyz) ).find('li:last').append(' <span>LIST LI ITEM</span> ').parent().html();
console.log(newString);

Open in new window

0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

743 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