Solved

jQuery: Append to last <li> in string

Posted on 2013-11-25
4
466 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 50 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 350 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 42

Accepted Solution

by:
Chris Stanyon earned 100 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now