Solved

jQuery: Append to last <li> in string

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
How to escape unsafe HTML tags 4 36
selected value 16 26
How do I update select listbox after search 2 26
Difference between Nuget Package and using Bower/Node.Js/Gulp 2 15
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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…

730 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