There is an element in <body>
<p><input id="btn" name="btn" type="submit" value="Click"></p>
and <ol id="list"></ol>
. When you click a button, a list should be created, processed by the function:
$('#btn').click(function(e){ $('<div id="divM1"/>').appendTo($( "#list" )); $('<span />').addClass('expandEditor ui-icon ui-icon-triangle-1-n').appendTo($( "#divM1" )); $('<span id="span1"/>').appendTo($( "#divM1" )); $('<span id="span2"/>').addClass('itemTitle').appendTo($( "#span1" )); $('<input type="text" value="Name menu"/>').addClass('textDiv').appendTo($( "#span2" )); $('<div id="divM2"/>').addClass('menuEdit hidden').appendTo($( "#divM1" )); $('<p id="par"/>').appendTo($( "#divM2" )); $('<input type="text" value="MyText"/>').addClass('text').appendTo($( "#par" )); });
In the third line, the expandEditor class should be applied to the span. But it does not work.
Js:
$('.expandEditor').attr('title','Click to show/hide item editor'); $('.expandEditor, .itemTitle').click(function(){ var id = $(this).attr('data-id'); $('#menuEdit'+id).toggle(); $(this).toggleClass('ui-icon-triangle-1-n').toggleClass('ui-icon-triangle-1-s'); });
How can I apply class properties to a tag? Maybe there is a more convenient way to add elements to the page?