I have this as a test harness:
<html>
<script src="jquery-1.7.1.min.js"></script>
<script src="jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("li").click(function () {
var id = $(this).attr("nodeid");
var path = window.location + "/SelectNode/" + id;
$.getJSON(path, function (data, status) {
alert(id + " : " + data);
//$("#" + id).text("<ul><li>" + data + "</li></ul>");
});
});
});
</script>
<title>JS Test</title>
<head>
JS Test</head>
<body>
<div">
<ul>
<li nodeid="1">item 1</li>
<li nodeid="2">item 2</li>
<li nodeid="3">item 3</li>
</ul>
</div>
</body>
</html>
I want to be able to add a new child list to the ‘clicked’ li. For the life of me I can’t figure out the syntax of injecting html after the specific li item clicked. The intention is to add an entire
<ul>
<li>injected item</li>
</ul>
after the the selected li. The commented out bit is my feeble attempt at it. It goes without saying that I am a jQuery beginner. I am throughly confused after reading the docs so need some people interaction to straigten me out here…
First, is the
nodeidattribute valid HTML? I don’t think so. Usedata-nodeid, this would be valid HTML5. (Why don’t you use a normalid? You are mixingnodeidandid. Anyway, it’s possible withnodeid. read on.)If you want to add HTML code you can’t use
.text(). There are many ways to add HTML code, this is an example with.append: http://jsfiddle.net/Vxaeb/1/(Just an example)
You get the
data-nodeidwithvar id = $(this).attr("data-nodeid");.Access the element with
$('li[data-nodeid="'+id+'"]').HTML:
Full JS:
Here a jsFiddle with your code AND
nodeid(as you mentioned you needed that) AND valid HTML5:http://jsfiddle.net/Vxaeb/3/