Solved

jQuery: Append to last <li> in string

Posted on 2013-11-25
4
472 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 43

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery CSS Scroll Issue 3 70
Bootstap Icons 3 40
Change text to radio button and calendar form 2 32
JQuery to parse xml string and get element by id 4 19
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)
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…

947 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