javascript - how to load sibling and there child attribute value -
i try code:-
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript" src="jquery.js"></script> <script> function load() { var select = $('#myselect'); var xml = ['<child_2 entity_id="2" value="root" parent_id="1">', '<child_4 entity_id="4" value="activities" parent_id="2">', '<child_10066 entity_id="10066" value="physical1" parent_id="4">', '<child_10067 entity_id="10067" value="cricket" parent_id="10066">', '<child_10068 entity_id="10068" value="one day" parent_id="10067"/>', '</child_10067>', '</child_10066>', '<child_10069 entity_id="10069" value="test2" parent_id="4"/>', '<child_10070 entity_id="10070" value="test3" parent_id="4"/>', '<child_10071 entity_id="10071" value="test4" parent_id="4"/>', '<child_10072 entity_id="10072" value="test5" parent_id="4"/>', '<child_5 entity_id="5" value="physical" parent_id="4"/>', '</child_4>', '</child_2>'].join(''); function makehtml($xml, $ul) { $(document).ready(function () { $(xml).find('child_10066').each(function () { var $node = $(this); var $li = $('<li></li>').html($node.attr('value')); $ul.append($li); if ($node.children().length > 0) { $childul = $('<ul></ul>').hide(); $ul.append($childul); // toggle hide , show $li.click(function () { if ($(this).next().css('display') == 'none') { $(this).next().show(); } else { $(this).next().hide(); } }); makehtml($node.children(), $childul); } }); }); } makehtml($(xml), $('ul')); } </script> <body onload="load()"> <div id="myselect"></div> <ul></ul> </body>
i want output:- (at file load time)
physical1
test2
test3
test4
test5
physical
if click on physical1
display there child node like:-
physical1
cricket
test2
test3
test4
test5
physical
if click on cricket
physical1
cricket
one day
test2
test3
test4
test5
physical
with use of code:- getting physical1
after click on them return new ul
li
, there value physical1
thanks...
try way!
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var select = $('#myselect'); var xml = ['<child_2 entity_id="2" value="root" parent_id="1">', '<child_4 entity_id="4" value="activities" parent_id="2">', '<child_10066 entity_id="10066" value="physical1" parent_id="4">', '<child_10067 entity_id="10067" value="cricket" parent_id="10066">', '<child_10068 entity_id="10068" value="one day" parent_id="10067"/>', '</child_10067>', '</child_10066>', '<child_10069 entity_id="10069" value="test2" parent_id="4"/>', '<child_10070 entity_id="10070" value="test3" parent_id="4"/>', '<child_10071 entity_id="10071" value="test4" parent_id="4"/>', '<child_10072 entity_id="10072" value="test5" parent_id="4"/>', '<child_5 entity_id="5" value="physical" parent_id="4"/>', '</child_4>', '</child_2>' ].join(''); function makehtml(parent, ul) { $(xml).find(parent).children().each(function() { var $node = $(this) ; var $li = $('<li/>'); var $span = $('<span/>'); $span.text($node.attr('value')); $li.append( $span ); ul.append( $li ); if ( $node.children().length > 0 ) { // toggle hide , show $li.click( function(){ event.stopimmediatepropagation(); var _ul = $(this).children()[1]; if ( $(_ul).css('display') == 'none') { $(_ul).show(); } else { $(_ul).hide(); } }); var _newul = $('<ul/>'); $(_newul).hide(); $li.append(_newul); makehtml($node.attr('tagname'), $(_newul)); } }); } makehtml('child_4', $('#_ul')); }); </script> <body> <div id="myselect"></div> <ul id="_ul"></ul> </body>
Comments
Post a Comment