?
Solved

jQuery: Append to last <li> in string

Posted on 2013-11-25
4
Medium Priority
?
541 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
  • 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 83

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 83

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 45

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

755 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