Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

jQuery: Append to last <li> in string

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

SOLUTION
Avatar of Mohit Vijay
Mohit Vijay
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leakim971
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial