Consider the following DOM manipulation problem: I need to create a span tag after clicking on some link. Because of speeding up the process I prefer using document.createElement()(pure Javascript) instead of the Jquery methods of creating HTML elements. The following code compiles properly but isn’t working (maybe because of interferention between Javascript and Jquery). Any help will be greatly appreciated.
<body>
<a href="#">Create thumbnails</a>
<script type="text/javascript">
$('a').click(function(e) {
var newSpan=null;
newSpan = document.createElement('span');
newSpan.setAttribute('class','themes');
$('.themes').html('themes');
$('.themes').appendTo('body');
return false;
});
</script>
</body>
Try the following:
Using
$(".themes")as a selector will search the document for elements with the class “theme”, not thenewSpanelement in your function. You have to usenewSpanas your selector until it is appended to your document.